Skip to content

Commit

Permalink
refactor(remove last user message): use a smarter way to remove the l…
Browse files Browse the repository at this point in the history
…ast user message if it's in the response
  • Loading branch information
MarcMcIntosh committed Feb 21, 2024
1 parent 6ee943d commit 322cbdc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
13 changes: 0 additions & 13 deletions src/events/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export enum EVENT_NAMES_TO_CHAT {
RECEIVE_AT_COMMAND_COMPLETION = "chat_receive_at_command_completion",
RECEIVE_AT_COMMAND_PREVIEW = "chat_receive_at_command_preview",
SET_SELECTED_AT_COMMAND = "chat_set_selected_command",
REMOVE_LAST_USER_MESSAGE = "chat_remove_last_user_message",
SET_LAST_MODEL_USED = "chat_set_last_model_used",
SET_SELECTED_SNIPPET = "chat_set_selected_snippet",
REMOVE_PREVIEW_FILE_BY_NAME = "chat_remove_file_from_preview",
Expand Down Expand Up @@ -195,18 +194,6 @@ export function isActionToChat(action: unknown): action is ActionToChat {
return Object.values(EVENT_NAMES).includes(action.type);
}

export interface RemoveLastUserMessage extends ActionToChat {
type: EVENT_NAMES_TO_CHAT.REMOVE_LAST_USER_MESSAGE;
payload: { id: string };
}

export function isRemoveLastUserMessage(
action: unknown,
): action is RemoveLastUserMessage {
if (!isActionToChat(action)) return false;
return action.type === EVENT_NAMES_TO_CHAT.REMOVE_LAST_USER_MESSAGE;
}

export interface ReceiveAtCommandCompletion extends ActionToChat {
type: EVENT_NAMES_TO_CHAT.RECEIVE_AT_COMMAND_COMPLETION;
payload: { id: string } & CommandCompletionResponse;
Expand Down
29 changes: 11 additions & 18 deletions src/hooks/useEventBusForChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
SetSelectedAtCommand,
isSetSelectedAtCommand,
isReceiveAtCommandPreview,
isRemoveLastUserMessage,
isChatUserMessageResponse,
isChatSetLastModelUsed,
isSetSelectedSnippet,
Expand Down Expand Up @@ -85,6 +84,8 @@ function reducer(state: ChatState, action: ActionToChat): ChatState {
const isThisChat =
action.payload?.id && action.payload.id === state.chat.id ? true : false;

console.log(action.type, { isThisChat, payload: action.payload });

Check warning on line 87 in src/hooks/useEventBusForChat.ts

View workflow job for this annotation

GitHub Actions / build (lts/*)

Unexpected console statement

Check warning on line 87 in src/hooks/useEventBusForChat.ts

View workflow job for this annotation

GitHub Actions / build (latest)

Unexpected console statement

if (isThisChat && isSetDisableChat(action)) {
return {
...state,
Expand All @@ -94,7 +95,11 @@ function reducer(state: ChatState, action: ActionToChat): ChatState {
}

if (isThisChat && isResponseToChat(action)) {
const messages = formatChatResponse(state.chat.messages, action.payload);
const hasUserMessage = isChatUserMessageResponse(action.payload);
const current = hasUserMessage
? state.chat.messages.slice(0, state.previous_message_length)
: state.chat.messages;
const messages = formatChatResponse(current, action.payload);
return {
...state,
waiting_for_response: false,
Expand Down Expand Up @@ -212,7 +217,10 @@ function reducer(state: ChatState, action: ActionToChat): ChatState {
...state,
streaming: false,
waiting_for_response: false,
error: action.payload.message,
error:
typeof action.payload.message === "string"
? action.payload.message
: "Error streaming",
};
}

Expand Down Expand Up @@ -332,21 +340,6 @@ function reducer(state: ChatState, action: ActionToChat): ChatState {
};
}

if (isThisChat && isRemoveLastUserMessage(action)) {
const messages = state.chat.messages.slice(
0,
state.previous_message_length,
);

return {
...state,
chat: {
...state.chat,
messages,
},
};
}

// TODO: this may need to be set by the editor
if (isThisChat && isChatSetLastModelUsed(action)) {
return {
Expand Down
8 changes: 0 additions & 8 deletions src/hooks/useEventBusForHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
isRequestAtCommandCompletion,
ReceiveAtCommandCompletion,
ReceiveAtCommandPreview,
RemoveLastUserMessage,
} from "../events";
import { useConfig } from "../contexts/config-context";

Expand Down Expand Up @@ -191,13 +190,6 @@ function handleSend(
const reader = response.body?.getReader();
if (!reader) return;

const removeLastUserMessage: RemoveLastUserMessage = {
type: EVENT_NAMES_TO_CHAT.REMOVE_LAST_USER_MESSAGE,
payload: { id: chat.id },
};

window.postMessage(removeLastUserMessage, "*");

return reader.read().then(function pump({ done, value }): Promise<void> {
if (done) {
// Do something with last chunk of data then exit reader
Expand Down

0 comments on commit 322cbdc

Please sign in to comment.