diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 9de97f4ac..923f4c9b5 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "license": "Apache-2.0", "dependencies": { "@electron/rebuild": "^3.2.10", @@ -115,7 +115,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.7", + "dotenv": "^16.4.5", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2", diff --git a/gui/package-lock.json b/gui/package-lock.json index af3d5cafa..91410e6df 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -121,7 +121,7 @@ "comment-json": "^4.2.3", "dbinfoz": "^0.14.0", "diff": "^7.0.0", - "dotenv": "^16.4.7", + "dotenv": "^16.4.5", "fastest-levenshtein": "^1.0.16", "follow-redirects": "^1.15.5", "google-auth-library": "^9.14.2", diff --git a/gui/src/context/IdeMessenger.ts b/gui/src/context/IdeMessenger.ts index bddd3fbbe..fbbfc21b1 100644 --- a/gui/src/context/IdeMessenger.ts +++ b/gui/src/context/IdeMessenger.ts @@ -37,14 +37,14 @@ export interface IIdeMessenger { messageType: T, data: FromWebviewProtocol[T][0], cancelToken?: AbortSignal, - ): FromWebviewProtocol[T][1]; + ): AsyncGenerator; llmStreamChat( modelTitle: string, cancelToken: AbortSignal | undefined, messages: ChatMessage[], options?: LLMFullCompletionOptions, - ): AsyncGenerator; + ): AsyncGenerator; ide: IDE; } @@ -147,11 +147,20 @@ export class IdeMessenger implements IIdeMessenger { }); } + /** + * Because of weird type stuff, we're actually yielding an array of the things + * that are streamed. For example, if the return type here says + * AsyncGenerator, then it's actually AsyncGenerator. + * This needs to be handled by the caller. + * + * Using unknown for now to make this more explicit + */ async *streamRequest( messageType: T, data: FromWebviewProtocol[T][0], cancelToken?: AbortSignal, - ): FromWebviewProtocol[T][1] { + ): AsyncGenerator { + // ): FromWebviewProtocol[T][1] { const messageId = uuidv4(); this.post(messageType, data, messageId); @@ -181,17 +190,16 @@ export class IdeMessenger implements IIdeMessenger { while (!done) { if (buffer.length > index) { - const chunk = buffer[index]; - index++; - yield chunk; + const chunks = buffer.slice(index); + index = buffer.length; + yield chunks; } await new Promise((resolve) => setTimeout(resolve, 50)); } - while (buffer.length > index) { - const chunk = buffer[index]; - index++; - yield chunk; + if (buffer.length > index) { + const chunks = buffer.slice(index); + yield chunks; } return returnVal; @@ -202,7 +210,7 @@ export class IdeMessenger implements IIdeMessenger { cancelToken: AbortSignal | undefined, messages: ChatMessage[], options: LLMFullCompletionOptions = {}, - ): AsyncGenerator { + ): AsyncGenerator { const gen = this.streamRequest( "llm/streamChat", { diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index 46e9e21f5..1b6f4d7f4 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -10,21 +10,21 @@ import { ApplyState, ChatHistoryItem, ChatMessage, + CodeToEdit, + ContextItem, ContextItemWithId, FileSymbolMap, - Session, - PromptLog, - CodeToEdit, - ToolCall, - ContextItem, MessageModes, + PromptLog, + Session, + ToolCall, } from "core"; import { incrementalParseJson } from "core/util/incrementalParseJson"; import { renderChatMessage } from "core/util/messageContent"; import { v4 as uuidv4 } from "uuid"; +import { RootState } from "../store"; import { streamResponseThunk } from "../thunks/streamResponse"; import { findCurrentToolCall } from "../util"; -import { RootState } from "../store"; // We need this to handle reorderings (e.g. a mid-array deletion) of the messages array. // The proper fix is adding a UUID to all chat messages, but this is the temp workaround. @@ -284,72 +284,74 @@ export const sessionSlice = createSlice({ state.streamAborter.abort(); state.streamAborter = new AbortController(); }, - streamUpdate: (state, action: PayloadAction) => { + streamUpdate: (state, action: PayloadAction) => { if (state.history.length) { - const lastMessage = state.history[state.history.length - 1]; + for (const message of action.payload) { + const lastMessage = state.history[state.history.length - 1]; - if ( - action.payload.role && - (lastMessage.message.role !== action.payload.role || - // This is when a tool call comes after assistant text - (lastMessage.message.content !== "" && - action.payload.role === "assistant" && - action.payload.toolCalls?.length)) - ) { - const baseHistoryItem = getBaseHistoryItem(); - - // Create a new message - const historyItem: ChatHistoryItemWithMessageId = { - ...baseHistoryItem, - message: { ...baseHistoryItem.message, ...action.payload }, - }; - - if (action.payload.role === "assistant" && action.payload.toolCalls) { - const [_, parsedArgs] = incrementalParseJson( - action.payload.toolCalls[0].function.arguments, - ); - historyItem.toolCallState = { - status: "generating", - toolCall: action.payload.toolCalls[0] as ToolCall, - toolCallId: action.payload.toolCalls[0].id, - parsedArgs, - }; - } - - state.history.push(historyItem); - } else { - // Add to the existing message - const msg = state.history[state.history.length - 1].message; - if (action.payload.content) { - msg.content += renderChatMessage(action.payload); - } else if ( - action.payload.role === "assistant" && - action.payload.toolCalls && - msg.role === "assistant" + if ( + message.role && + (lastMessage.message.role !== message.role || + // This is when a tool call comes after assistant text + (lastMessage.message.content !== "" && + message.role === "assistant" && + message.toolCalls?.length)) ) { - if (!msg.toolCalls) { - msg.toolCalls = []; + const baseHistoryItem = getBaseHistoryItem(); + + // Create a new message + const historyItem: ChatHistoryItemWithMessageId = { + ...baseHistoryItem, + message: { ...baseHistoryItem.message, ...message }, + }; + + if (message.role === "assistant" && message.toolCalls) { + const [_, parsedArgs] = incrementalParseJson( + message.toolCalls[0].function.arguments, + ); + historyItem.toolCallState = { + status: "generating", + toolCall: message.toolCalls[0] as ToolCall, + toolCallId: message.toolCalls[0].id, + parsedArgs, + }; } - action.payload.toolCalls.forEach((toolCall, i) => { - if (msg.toolCalls.length <= i) { - msg.toolCalls.push(toolCall); - } else { - msg.toolCalls[i].function.arguments += - toolCall.function.arguments; - const [_, parsedArgs] = incrementalParseJson( - msg.toolCalls[i].function.arguments, - ); - - state.history[ - state.history.length - 1 - ].toolCallState.parsedArgs = parsedArgs; - state.history[ - state.history.length - 1 - ].toolCallState.toolCall.function.arguments += - toolCall.function.arguments; + 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); + } else if ( + message.role === "assistant" && + message.toolCalls && + msg.role === "assistant" + ) { + if (!msg.toolCalls) { + msg.toolCalls = []; } - }); + message.toolCalls.forEach((toolCall, i) => { + if (msg.toolCalls.length <= i) { + msg.toolCalls.push(toolCall); + } else { + msg.toolCalls[i].function.arguments += + toolCall.function.arguments; + + const [_, parsedArgs] = incrementalParseJson( + msg.toolCalls[i].function.arguments, + ); + + state.history[ + state.history.length - 1 + ].toolCallState.parsedArgs = parsedArgs; + state.history[ + state.history.length - 1 + ].toolCallState.toolCall.function.arguments += + toolCall.function.arguments; + } + }); + } } } } diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index 3c3c49222..5a22a36b5 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -1,6 +1,7 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { ChatMessage, PromptLog } from "core"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; +import { selectDefaultModel } from "../slices/configSlice"; import { abortStream, addPromptCompletionPair, @@ -10,7 +11,6 @@ import { } from "../slices/sessionSlice"; import { ThunkApiType } from "../store"; import { callTool } from "./callTool"; -import { selectDefaultModel } from "../slices/configSlice"; export const streamNormalInput = createAsyncThunk< void, @@ -56,14 +56,9 @@ export const streamNormalInput = createAsyncThunk< break; } - const update = next.value as ChatMessage; - dispatch(streamUpdate(update)); + const updates = next.value as ChatMessage[]; + dispatch(streamUpdate(updates)); next = await gen.next(); - - // There has been lag when streaming tool calls. This is a temporary solution - if (update.role === "assistant" && update.toolCalls) { - await new Promise((resolve) => setTimeout(resolve, 10)); - } } // Attach prompt log diff --git a/gui/src/redux/thunks/streamResponseAfterToolCall.ts b/gui/src/redux/thunks/streamResponseAfterToolCall.ts index 0fe588f56..52b15c13f 100644 --- a/gui/src/redux/thunks/streamResponseAfterToolCall.ts +++ b/gui/src/redux/thunks/streamResponseAfterToolCall.ts @@ -2,6 +2,7 @@ import { createAsyncThunk } from "@reduxjs/toolkit"; import { ChatMessage, ContextItem } from "core"; import { constructMessages } from "core/llm/constructMessages"; import { renderContextItems } from "core/util/messageContent"; +import { selectDefaultModel } from "../slices/configSlice"; import { addContextItemsAtIndex, setActive, @@ -11,7 +12,6 @@ import { ThunkApiType } from "../store"; import { handleErrors } from "./handleErrors"; import { resetStateForNewMessage } from "./resetStateForNewMessage"; import { streamNormalInput } from "./streamNormalInput"; -import { selectDefaultModel } from "../slices/configSlice"; export const streamResponseAfterToolCall = createAsyncThunk< void, @@ -39,7 +39,7 @@ export const streamResponseAfterToolCall = createAsyncThunk< toolCallId, }; - dispatch(streamUpdate(newMessage)); + dispatch(streamUpdate([newMessage])); dispatch( addContextItemsAtIndex({ index: initialHistory.length, diff --git a/gui/src/redux/thunks/streamSlashCommand.ts b/gui/src/redux/thunks/streamSlashCommand.ts index 314b5cc53..79336bb3d 100644 --- a/gui/src/redux/thunks/streamSlashCommand.ts +++ b/gui/src/redux/thunks/streamSlashCommand.ts @@ -5,9 +5,9 @@ import { RangeInFile, SlashCommandDescription, } from "core"; -import { ThunkApiType } from "../store"; -import { abortStream, streamUpdate } from "../slices/sessionSlice"; import { selectDefaultModel } from "../slices/configSlice"; +import { abortStream, streamUpdate } from "../slices/sessionSlice"; +import { ThunkApiType } from "../store"; export const streamSlashCommand = createAsyncThunk< void, @@ -62,8 +62,17 @@ export const streamSlashCommand = createAsyncThunk< dispatch(abortStream()); break; } - if (typeof update === "string") { - dispatch(streamUpdate(update)); + for (const item of update) { + if (typeof item === "string") { + dispatch( + streamUpdate([ + { + role: "assistant", + content: item, + }, + ]), + ); + } } } clearInterval(checkActiveInterval);