diff --git a/core/core.ts b/core/core.ts index 5650c3a25..415540a74 100644 --- a/core/core.ts +++ b/core/core.ts @@ -402,12 +402,6 @@ export class Core { if (config.experimental?.readResponseTTS && "completion" in next.value) { void TTS.read(next.value?.completion); } - - // TODO: better detection of when we should prompt for a title; currently only doing it on the user's first chat - if (config.experimental?.getChatTitles && "completion" in next.value && msg.data.messages.filter(m => m.role === "user").length === 1) { - void ChatDescriber.describe(model, msg.data.completionOptions, next.value?.completion); - } - return { done: true, content: next.value }; } @@ -489,6 +483,11 @@ export class Core { on("tts/kill", async () => { void TTS.kill(); }); + + on("chatDescriber/describe", async (msg) => { + const currentModel = await this.getSelectedModel(); + return await ChatDescriber.describe(currentModel, {}, msg.data); + }); async function* runNodeJsSlashCommand( configHandler: ConfigHandler, diff --git a/core/protocol/core.ts b/core/protocol/core.ts index d5920ca81..e2d512950 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -131,6 +131,7 @@ export type ToCoreFromIdeOrWebviewProtocol = { }, ProtocolGeneratorType, ]; + "chatDescriber/describe": [string, string | undefined]; "stats/getTokensPerDay": [ undefined, { day: string; promptTokens: number; generatedTokens: number }[], diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 3b0b79037..f570b9e87 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -36,6 +36,7 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "llm/streamChat", "llm/listModels", "streamDiffLines", + "chatDescriber/describe", "stats/getTokensPerDay", "stats/getTokensPerModel", "index/setPaused", @@ -58,6 +59,5 @@ export const CORE_TO_WEBVIEW_PASS_THROUGH: (keyof ToWebviewFromCoreProtocol)[] = "refreshSubmenuItems", "isContinueInputFocused", "didChangeAvailableProfiles", - "setCurrentChatTitle", "setTTSActive", ]; diff --git a/core/protocol/webview.ts b/core/protocol/webview.ts index 4a41becb1..d3b127fdc 100644 --- a/core/protocol/webview.ts +++ b/core/protocol/webview.ts @@ -14,5 +14,4 @@ export type ToWebviewFromIdeOrCoreProtocol = { void, ]; setTTSActive: [boolean, void]; - setCurrentChatTitle: [string, void]; }; diff --git a/core/util/chatDescriber.ts b/core/util/chatDescriber.ts index acb850d15..7a4f2d546 100644 --- a/core/util/chatDescriber.ts +++ b/core/util/chatDescriber.ts @@ -2,6 +2,7 @@ import type { IMessenger } from "./messenger"; import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; import { ILLM, LLMFullCompletionOptions } from ".."; import { stripImages } from "../llm/images"; +import { removeQuotesAndEscapes } from "."; /** * Removes code blocks from a message. @@ -19,14 +20,14 @@ function removeCodeBlocksAndTrim(msgText: string): string { export class ChatDescriber { static prompt: string | undefined = - "Given the following... please reply with a short summary that is 4-12 words in length, summarizing what the user is asking for OR what the user is trying to accomplish. You should only respond with the summary, no additional text or explanation.\n\n"; + "Given the following... please reply with a short summary that is 4-12 words in length, you should summarize what the user is asking for OR what the user is trying to accomplish. You should only respond with the summary, no additional text or explanation, you don't need ending punctuation.\n\n"; static messenger: IMessenger; static async describe( model: ILLM, completionOptions: LLMFullCompletionOptions, message: string, - ) { + ): Promise { if (!ChatDescriber.prompt) { return; } @@ -53,7 +54,7 @@ export class ChatDescriber { ); // Set the title - ChatDescriber.messenger?.send("setCurrentChatTitle", titleResponse.content.toString()); + return removeQuotesAndEscapes(titleResponse.content.toString()); } // // TODO: Allow the user to manually set specific/tailored prompts to generate their titles diff --git a/gui/src/hooks/useHistory.tsx b/gui/src/hooks/useHistory.tsx index 7278d052c..a47b54c25 100644 --- a/gui/src/hooks/useHistory.tsx +++ b/gui/src/hooks/useHistory.tsx @@ -33,6 +33,14 @@ function useHistory(dispatch: Dispatch) { return result.status === "success" ? result.content : []; } + async function getChatTitle(message?: string): Promise { + const result = await ideMessenger.request( + "chatDescriber/describe", + message, + ); + return result.status === "success" ? result.content : undefined; + } + async function saveSession() { if (state.history.length === 0) return; @@ -40,7 +48,25 @@ function useHistory(dispatch: Dispatch) { dispatch(newSession()); await new Promise((resolve) => setTimeout(resolve, 10)); - // Set default title if it isn't set (=== "New Session"), if it is set then use that title, fallback use the first user message + if ( + state.config?.experimental?.getChatTitles && + stateCopy.title === "New Session" + ) { + try { + // Check if we have first assistant response + let assistantResponse = stateCopy.history + ?.filter((h) => h.message.role === "assistant")[0] + ?.message?.content?.toString(); + + if (assistantResponse) { + stateCopy.title = await getChatTitle(assistantResponse); + } + } catch (e) { + throw new Error("Unable to get chat title"); + } + } + + // Fallback if we get an error above or if the user has not set getChatTitles let title = stateCopy.title === "New Session" ? truncateText( diff --git a/gui/src/hooks/useSetup.ts b/gui/src/hooks/useSetup.ts index cd583768f..d3c8ef691 100644 --- a/gui/src/hooks/useSetup.ts +++ b/gui/src/hooks/useSetup.ts @@ -9,8 +9,7 @@ import { setConfig, setInactive, setSelectedProfileId, - setTTSActive, - setCurrentChatTitle + setTTSActive } from "../redux/slices/stateSlice"; import { RootState } from "../redux/store"; @@ -95,10 +94,6 @@ function useSetup(dispatch: Dispatch) { dispatch(setTTSActive(status)); }); - useWebviewListener("setCurrentChatTitle", async (newTitle) => { - dispatch(setCurrentChatTitle(newTitle)); - }); - useWebviewListener("setColors", async (colors) => { Object.keys(colors).forEach((key) => { document.body.style.setProperty(key, colors[key]); diff --git a/gui/src/redux/slices/stateSlice.ts b/gui/src/redux/slices/stateSlice.ts index 6f67b639e..b97bb0755 100644 --- a/gui/src/redux/slices/stateSlice.ts +++ b/gui/src/redux/slices/stateSlice.ts @@ -159,9 +159,6 @@ export const stateSlice = createSlice({ } // TODO }, - setCurrentChatTitle: (state, { payload }: PayloadAction) => { - state.title = payload; - }, addContextItems: (state, action: PayloadAction) => { state.contextItems = state.contextItems.concat(action.payload); }, @@ -462,8 +459,7 @@ export const { consumeMainEditorContent, setSelectedProfileId, deleteMessage, - setIsGatheringContext, - setCurrentChatTitle + setIsGatheringContext } = stateSlice.actions; export default stateSlice.reducer;