diff --git a/core/index.d.ts b/core/index.d.ts index 766c826b7..56b610101 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -34,6 +34,11 @@ export interface ILLM extends LLMOptions { options?: LLMFullCompletionOptions ): AsyncGenerator; + chat( + messages: ChatMessage[], + options?: LLMFullCompletionOptions + ): Promise; + countTokens(text: string): number; } diff --git a/core/llm/index.ts b/core/llm/index.ts index b293b2b2d..c4e85b720 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -1,5 +1,6 @@ import { ChatMessage, + ChatMessageRole, CompletionOptions, ILLM, LLMFullCompletionOptions, @@ -374,6 +375,14 @@ export abstract class BaseLLM implements ILLM { return completion; } + async chat(messages: ChatMessage[], options: LLMFullCompletionOptions = {}) { + let completion = ""; + for await (const chunk of this.streamChat(messages, options)) { + completion += chunk.content; + } + return { role: "assistant" as ChatMessageRole, content: completion }; + } + async *streamChat( messages: ChatMessage[], options: LLMFullCompletionOptions = {} diff --git a/core/llm/llms/FreeTrial.ts b/core/llm/llms/FreeTrial.ts index c1cd9686c..89c1c3381 100644 --- a/core/llm/llms/FreeTrial.ts +++ b/core/llm/llms/FreeTrial.ts @@ -77,6 +77,10 @@ class FreeTrial extends BaseLLM { }), }); + if (response.status !== 200) { + throw new Error(await response.text()); + } + const reader = response.body?.getReader(); let decoder = new TextDecoder("utf-8"); diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 54e41e6dd..36be6aa2d 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -83,6 +83,10 @@ class OpenAI extends BaseLLM { }), }); + if (response.status !== 200) { + throw new Error(await response.text()); + } + // Receive as SSE if (response.body === null) { return; diff --git a/gui/src/components/Layout.tsx b/gui/src/components/Layout.tsx index ac89cea8d..77dd0dc6c 100644 --- a/gui/src/components/Layout.tsx +++ b/gui/src/components/Layout.tsx @@ -14,7 +14,6 @@ import { vscBackground, vscForeground, } from "."; -import useHistory from "../hooks/useHistory"; import { defaultModelSelector } from "../redux/selectors/modelSelectors"; import { setBottomMessage, @@ -151,8 +150,6 @@ const Layout = () => { }; }, [timeline]); - const { saveSession } = useHistory(); - useEffect(() => { const handler = (event: any) => { if (event.data.type === "addModel") { diff --git a/gui/src/components/gui/StepContainer.tsx b/gui/src/components/gui/StepContainer.tsx index b200f9f47..05edc37ea 100644 --- a/gui/src/components/gui/StepContainer.tsx +++ b/gui/src/components/gui/StepContainer.tsx @@ -15,6 +15,7 @@ import { import { RootStore } from "../../redux/store"; import { getFontSize } from "../../util"; import { logDevData } from "../../util/ide"; +import HeaderButtonWithText from "../HeaderButtonWithText"; import StyledMarkdownPreview from "../markdown/StyledMarkdownPreview"; interface StepContainerProps { @@ -108,32 +109,36 @@ function StepContainer(props: StepContainerProps) { {(isHovered || typeof feedback !== "undefined") && !active && (
{feedback === false || ( - { - sendFeedback(true); - }} - /> + + { + sendFeedback(true); + }} + /> + )} {feedback === true || ( - { - sendFeedback(false); - }} - /> + + { + sendFeedback(false); + }} + /> + )}
)} diff --git a/gui/src/components/index.ts b/gui/src/components/index.ts index dc5c00cfd..b6991b7bf 100644 --- a/gui/src/components/index.ts +++ b/gui/src/components/index.ts @@ -7,8 +7,7 @@ export const lightGray = "#646464"; export const buttonColor = "#1bbe84"; export const buttonColorHover = "#1bbe84a8"; -export const secondaryDark = - "var(--vscode-list-hoverBackground, rgb(45 45 45))"; +export const secondaryDark = "var(--vscode-input-background, rgb(45 45 45))"; // export const vscBackground = "var(--vscode-editor-background, rgb(30 30 30))"; export const vscBackground = "var(--vscode-sideBar-background, rgb(30 30 30))"; export const vscForeground = "var(--vscode-editor-foreground, #fff)"; diff --git a/gui/src/components/mainInput/ComboBox.tsx b/gui/src/components/mainInput/ComboBox.tsx index 2fd7e4e65..072749d4c 100644 --- a/gui/src/components/mainInput/ComboBox.tsx +++ b/gui/src/components/mainInput/ComboBox.tsx @@ -48,7 +48,6 @@ import { addContextItems, addContextItemsAtIndex, deleteContextWithIds, - newSession, setInactive, } from "../../redux/slices/stateSlice"; import { setBottomMessage } from "../../redux/slices/uiStateSlice"; @@ -371,7 +370,6 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { const [miniSearch, firstResults] = useContext(SearchContext); const dispatch = useDispatch(); - const workspacePaths = (window as any).workspacePaths || []; const state = useSelector((state: RootStore) => state.state); const [history, setHistory] = React.useState([]); @@ -711,7 +709,7 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { }; }, []); - const { saveSession } = useHistory(); + const { saveSession } = useHistory(dispatch); useEffect(() => { if (!inputRef.current || !props.isMainInput) { @@ -725,14 +723,12 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { inputRef.current!.focus(); if (state.history.length > 0) { saveSession(); - dispatch(newSession()); } dispatch(setTakenActionTrue(null)); } else if (event.data.type === "focusContinueInputWithEdit") { inputRef.current!.focus(); if (state.history.length > 0) { saveSession(); - dispatch(newSession()); } if (!inputRef.current?.value.startsWith("/edit")) { @@ -741,7 +737,6 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { dispatch(setTakenActionTrue(null)); } else if (event.data.type === "focusContinueInputWithNewSession") { saveSession(); - dispatch(newSession()); dispatch(setTakenActionTrue(null)); } }; @@ -1221,7 +1216,7 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { fontSize={getFontSize()} disabled={props.disabled} placeholder={ - history.length === 0 + state.history.length === 0 ? `Ask a question, '/' for slash commands, '@' to add context` : `Ask a follow-up` } @@ -1665,7 +1660,6 @@ const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { { saveSession(); - dispatch(newSession()); }} className="mr-auto" > diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 0ad51e877..b896bf7d9 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -142,6 +142,7 @@ function useChatHandler(dispatch: Dispatch) { logs.push(log); }; defaultModel.writeLog = writeLog; + if (!commandAndInput) { await _streamNormalInput(messages); } else { @@ -149,17 +150,17 @@ function useChatHandler(dispatch: Dispatch) { await _streamSlashCommand(messages, slashCommand, commandInput); } - defaultModel.writeLog = undefined; const pairedLogs = []; for (let i = 0; i < logs.length; i += 2) { pairedLogs.push([logs[i], logs[i + 1]]); } dispatch(addLogs(pairedLogs)); - - dispatch(setInactive()); } catch (e) { console.log("Continue: error streaming response: ", e); errorPopup(e.message); + } finally { + dispatch(setInactive()); + defaultModel.writeLog = undefined; } } diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index 8105184ad..084a73dcf 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -1,6 +1,9 @@ +import { Dispatch } from "@reduxjs/toolkit"; import { PersistedSessionInfo, SessionInfo } from "core"; import { ideRequest } from "core/ide/messaging"; import { useSelector } from "react-redux"; +import { defaultModelSelector } from "../redux/selectors/modelSelectors"; +import { newSession } from "../redux/slices/stateSlice"; import { RootStore } from "../redux/store"; function truncateText(text: string, maxLength: number) { @@ -10,8 +13,9 @@ function truncateText(text: string, maxLength: number) { return text; } -function useHistory() { +function useHistory(dispatch: Dispatch) { const state = useSelector((state: RootStore) => state.state); + const defaultModel = useSelector(defaultModelSelector); async function getHistory(): Promise { return await ideRequest("history", {}); @@ -19,13 +23,28 @@ function useHistory() { async function saveSession() { if (state.history.length === 0) return; + + const stateCopy = { ...state }; + dispatch(newSession()); + await new Promise((resolve) => setTimeout(resolve, 10)); + + const { content: title } = await defaultModel.chat( + [ + ...stateCopy.history.map((item) => item.message), + { + role: "user", + content: + "Give a maximum 40 character title to describe this conversation so far. The title should help me recall the conversation if I look for it later. DO NOT PUT QUOTES AROUND THE TITLE", + }, + ], + { maxTokens: 20 } + ); + const sessionInfo: PersistedSessionInfo = { - history: state.history, - title: - state.title === "New Session" - ? truncateText(state.history[0].message.content, 50) - : state.title, - sessionId: state.sessionId, + history: stateCopy.history, + title, + // truncateText(stateCopy.history[0].message.content, 50) + sessionId: stateCopy.sessionId, workspaceDirectory: (window as any).workspacePaths?.[0] || "", }; return await ideRequest("saveSession", sessionInfo); diff --git a/gui/src/pages/gui.tsx b/gui/src/pages/gui.tsx index 366f953cb..c0c6ccb22 100644 --- a/gui/src/pages/gui.tsx +++ b/gui/src/pages/gui.tsx @@ -335,13 +335,12 @@ function GUI(props: GUIProps) { ] ); - const { saveSession } = useHistory(); + const { saveSession } = useHistory(dispatch); useEffect(() => { const handler = (event: any) => { if (event.data.type === "newSession") { saveSession(); - dispatch(newSession()); mainTextInputRef.current?.focus?.(); } }; @@ -408,128 +407,137 @@ function GUI(props: GUIProps) { ); return ( - { - if (e.key === "Enter" && e.ctrlKey) { - onMainTextInput(); - } - }} - > -
- - {state.history.map((item, index: number) => { - return ( - - { - dispatch(newSession()); - }} - > - {item.message.role === "user" ? ( - { - if (value) { - dispatch(resubmitAtIndex({ index, content: value })); - streamResponse(value, index); + <> + { + if (e.key === "Enter" && e.ctrlKey) { + onMainTextInput(); + } + }} + > +
+ + {state.history.map((item, index: number) => { + return ( + + { + dispatch(newSession()); + }} + > + {item.message.role === "user" ? ( + { + if (value) { + dispatch( + resubmitAtIndex({ index, content: value }) + ); + streamResponse(value, index); + } + e?.stopPropagation(); + e?.preventDefault(); + }} + isToggleOpen={ + typeof stepsOpen[index] === "undefined" + ? true + : stepsOpen[index]! } - e?.stopPropagation(); - e?.preventDefault(); - }} - isToggleOpen={ - typeof stepsOpen[index] === "undefined" - ? true - : stepsOpen[index]! - } - index={index} - /> - ) : ( - - ) : false ? ( - + ) : ( + + ) : false ? ( + + ) : ( + + ) + } + open={ + typeof stepsOpen[index] === "undefined" + ? false + ? false + : true + : stepsOpen[index]! + } + onToggle={() => {}} + > + {false ? ( // Most of these falses were previously (step.error) + {}} + error={undefined} + onDelete={() => {}} /> ) : ( - - ) - } - open={ - typeof stepsOpen[index] === "undefined" - ? false - ? false - : true - : stepsOpen[index]! - } - onToggle={() => {}} - > - {false ? ( // Most of these falses were previously (step.error) - {}} - error={undefined} - onDelete={() => {}} - /> - ) : ( - {}} - item={item} - onReverse={() => {}} - onRetry={() => {}} - onDelete={() => {}} - /> - )} - - )} - {/*
*/} -
-
- ); - })} -
+ {}} + item={item} + onReverse={() => {}} + onRetry={() => {}} + onDelete={() => {}} + /> + )} + + )} + {/*
*/} + + + ); + })} + -
- {active ? ( - { - dispatch(setInactive()); - }} - > - {getMetaKeyLabel()} ⌫ Cancel - - ) : ( - { - onMainTextInput(e); - e?.stopPropagation(); - e?.preventDefault(); - }} - onInputValueChange={() => {}} - onToggleAddContext={() => {}} - /> - )} -
- +
+ {active || ( + { + onMainTextInput(e); + e?.stopPropagation(); + e?.preventDefault(); + }} + onInputValueChange={() => {}} + onToggleAddContext={() => {}} + /> + )} +
+ + {active && ( + { + dispatch(setInactive()); + }} + > + {getMetaKeyLabel()} ⌫ Cancel + + )} + ); } diff --git a/gui/src/pages/history.tsx b/gui/src/pages/history.tsx index 65392e99a..11f1368c8 100644 --- a/gui/src/pages/history.tsx +++ b/gui/src/pages/history.tsx @@ -37,8 +37,10 @@ const SectionHeader = styled.tr` width: 100%; font-weight: bold; text-align: center; + align-items: center; margin: 0; position: sticky; + height: 1.5em; `; const TdDiv = styled.div` @@ -65,7 +67,7 @@ function TableRow({ const workspacePaths = (window as any).workspacePaths || [""]; const [hovered, setHovered] = useState(false); - const { saveSession, deleteSession, loadSession } = useHistory(); + const { saveSession, deleteSession, loadSession } = useHistory(dispatch); return ( (null); const [headerHeight, setHeaderHeight] = useState(0); - const { getHistory } = useHistory(); + const dispatch = useDispatch(); + const { getHistory } = useHistory(dispatch); useEffect(() => { const fetchSessions = async () => { @@ -241,21 +244,21 @@ function History() { return ( {index === 0 && date > yesterday && ( - + Today )} {date < yesterday && date > lastWeek && prevDate > yesterday && ( - + This Week )} {date < lastWeek && date > lastMonth && prevDate > lastWeek && ( - + This Month )}