diff --git a/gui/src/components/markdown/utils.ts b/gui/src/components/markdown/utils.ts index 1044066d1..9b2329303 100644 --- a/gui/src/components/markdown/utils.ts +++ b/gui/src/components/markdown/utils.ts @@ -30,7 +30,7 @@ export function isTerminalCodeBlock( text: string, ) { return ( - terminalLanguages.includes(language) || + (language && terminalLanguages.includes(language)) || ((!language || language?.length === 0) && (text.trim().split("\n").length === 1 || commonTerminalCommands.some((c) => text.trim().startsWith(c)))) diff --git a/gui/src/context/IdeMessenger.ts b/gui/src/context/IdeMessenger.ts index 76881e9a7..46bc92538 100644 --- a/gui/src/context/IdeMessenger.ts +++ b/gui/src/context/IdeMessenger.ts @@ -164,18 +164,22 @@ export class IdeMessenger implements IIdeMessenger { messageType: T, data: FromWebviewProtocol[T][0], cancelToken?: AbortSignal, - ): AsyncGenerator { - // ): FromWebviewProtocol[T][1] { + ): AsyncGenerator { const messageId = uuidv4(); this.post(messageType, data, messageId); - const buffer: any[] = []; + const buffer: FromWebviewProtocol[T][1][] = []; let index = 0; let done = false; let returnVal = undefined; - const handler = (event: { data: Message }) => { + const handler = (event: { + data: Message<{ + done: boolean; + content: FromWebviewProtocol[T][1]; + }>; + }) => { if (event.data.messageId === messageId) { const responseData = event.data.data; if (responseData.done) { diff --git a/gui/src/context/SubmenuContextProviders.tsx b/gui/src/context/SubmenuContextProviders.tsx index 673caec1c..f99848665 100644 --- a/gui/src/context/SubmenuContextProviders.tsx +++ b/gui/src/context/SubmenuContextProviders.tsx @@ -144,7 +144,9 @@ export const SubmenuContextProvidersProvider = ({ [minisearches], ); - const lastOpenFilesRef = useRef([]); + const lastOpenFilesRef = useRef< + Awaited> + >([]); useEffect(() => { let isMounted = true; const refreshOpenFiles = async () => { @@ -214,12 +216,14 @@ export const SubmenuContextProvidersProvider = ({ try { const results = getSubmenuSearchResults(providerTitle, query); if (results.length === 0) { - const fallbackItems = (fallbackResults[providerTitle] ?? []) + const fallbackItems = ( + providerTitle ? (fallbackResults[providerTitle] ?? []) : [] + ) .slice(0, limit) .map((result) => { return { ...result, - providerTitle, + providerTitle: providerTitle || "unknown", }; }); diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 4888d5cb1..7cd06594b 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -281,16 +281,16 @@ export const sessionSlice = createSlice({ }, streamUpdate: (state, action: PayloadAction) => { if (state.history.length) { + const lastItem = state.history[state.history.length - 1]; + const lastMessage = lastItem.message; for (const message of action.payload) { - const lastMessage = state.history[state.history.length - 1]; - if ( message.role && - (lastMessage.message.role !== message.role || + (lastMessage.role !== message.role || // This is when a tool call comes after assistant text - (lastMessage.message.content !== "" && - message.role === "assistant" && - message.toolCalls?.length)) + (lastMessage.content !== "" && + message.role === "assistant" +)) ) { // Create a new message const historyItem: ChatHistoryItemWithMessageId = { @@ -300,8 +300,8 @@ export const sessionSlice = createSlice({ }, contextItems: [], }; - - if (message.role === "assistant" && message.toolCalls) { + const toolCalls = + if (message.role === "assistant" && message.toolCalls?.[0]) { const [_, parsedArgs] = incrementalParseJson( message.toolCalls[0].function.arguments, ); @@ -316,34 +316,29 @@ export const sessionSlice = createSlice({ state.history.push(historyItem); } else { // Add to the existing message - const msg = state.history[state.history.length - 1].message; if (message.content) { - msg.content += renderChatMessage(message); + lastMessage.content += renderChatMessage(message); } else if ( message.role === "assistant" && message.toolCalls && - msg.role === "assistant" + lastMessage.role === "assistant" ) { - if (!msg.toolCalls) { - msg.toolCalls = []; + if (!lastMessage.toolCalls) { + lastMessage.toolCalls = []; } message.toolCalls.forEach((toolCall, i) => { - if (msg.toolCalls.length <= i) { - msg.toolCalls.push(toolCall); + if (lastMessage.toolCalls!.length <= i) { + lastMessage.toolCalls!.push(toolCall); } else { - msg.toolCalls[i].function.arguments += + lastMessage.toolCalls[i].function.arguments += toolCall.function.arguments; const [_, parsedArgs] = incrementalParseJson( - msg.toolCalls[i].function.arguments, + lastMessage.toolCalls[i].function.arguments, ); - state.history[ - state.history.length - 1 - ].toolCallState.parsedArgs = parsedArgs; - state.history[ - state.history.length - 1 - ].toolCallState.toolCall.function.arguments += + lastItem.toolCallState.parsedArgs = parsedArgs; + lastItem.toolCallState.toolCall.function.arguments += toolCall.function.arguments; } }); diff --git a/gui/src/redux/store.ts b/gui/src/redux/store.ts index 5ddf55ee6..8a617d2e8 100644 --- a/gui/src/redux/store.ts +++ b/gui/src/redux/store.ts @@ -63,7 +63,10 @@ const persistConfig = { migrate: createMigrate(migrations, { debug: false }), }; -const persistedReducer = persistReducer(persistConfig, rootReducer); +const persistedReducer = persistReducer>( + persistConfig, + rootReducer, +); export function setupStore() { return configureStore({ diff --git a/gui/src/redux/thunks/callTool.ts b/gui/src/redux/thunks/callTool.ts index 184ad6e6a..7f9af5b04 100644 --- a/gui/src/redux/thunks/callTool.ts +++ b/gui/src/redux/thunks/callTool.ts @@ -14,10 +14,10 @@ export const callTool = createAsyncThunk( const state = getState(); const toolCallState = selectCurrentToolCall(state); - console.log("calling tool", toolCallState.toolCall); if (!toolCallState) { return; } + console.log("calling tool", toolCallState.toolCall); if (toolCallState.status !== "generated") { return; diff --git a/gui/src/redux/thunks/gatherContext.ts b/gui/src/redux/thunks/gatherContext.ts index 7f59a0547..125012479 100644 --- a/gui/src/redux/thunks/gatherContext.ts +++ b/gui/src/redux/thunks/gatherContext.ts @@ -39,8 +39,10 @@ export const gatherContext = createAsyncThunk< state.config.config.experimental?.defaultContext ?? []; if (!state.config.defaultModelTitle) { - console.error("Failed to gather context, no model selected"); - return; + console.error( + "gatherContext thunk: Cannot gather context, no model selected", + ); + throw new Error("No chat model selected"); } // Resolve context providers and construct new history diff --git a/gui/src/redux/thunks/session.ts b/gui/src/redux/thunks/session.ts index 9e4bbddc2..d9f35cfd6 100644 --- a/gui/src/redux/thunks/session.ts +++ b/gui/src/redux/thunks/session.ts @@ -192,7 +192,7 @@ export const saveCurrentSession = createAsyncThunk< } // Fallbacks if above doesn't work out or getChatTitles = false if (title === NEW_SESSION_TITLE) { - title = getChatTitleFromMessage(history[0].message); + title = getChatTitleFromMessage(state.session.history[0].message); } } // More fallbacks in case of no title diff --git a/gui/src/redux/thunks/streamResponse.ts b/gui/src/redux/thunks/streamResponse.ts index 3b70ecba1..0ad5f2fbe 100644 --- a/gui/src/redux/thunks/streamResponse.ts +++ b/gui/src/redux/thunks/streamResponse.ts @@ -66,6 +66,10 @@ export const streamResponseThunk = createAsyncThunk< const slashCommands = state.config.config.slashCommands || []; const inputIndex = index ?? state.session.history.length; + if (!defaultModel) { + throw new Error("No chat model selected"); + } + dispatch(submitEditorAndInitAtIndex({ index, editorState })); resetStateForNewMessage(); @@ -115,7 +119,6 @@ export const streamResponseThunk = createAsyncThunk< unwrapResult(await dispatch(streamNormalInput(messages))); } else { const [slashCommand, commandInput] = commandAndInput; - let updatedContextItems = []; posthog.capture("step run", { step_name: slashCommand.name, params: {}, @@ -132,7 +135,7 @@ export const streamResponseThunk = createAsyncThunk< input: commandInput, historyIndex: inputIndex, selectedCode, - contextItems: updatedContextItems, + contextItems: [], }), ); }