Skip to content

Commit

Permalink
fix(duplicated messages): when restoring chat set previous message le…
Browse files Browse the repository at this point in the history
…ngth to the index of the last assistant message
  • Loading branch information
MarcMcIntosh committed Mar 22, 2024
1 parent 777199a commit 987aa7e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/__fixtures__/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ export const MARS_ROVER_CHAT: ChatThread = {
// "Use backquotes for code blocks. Pay close attention to indent when editing code blocks: indent must be exactly the same as in the original code block.",
// ],
["user", "mars rover kata"],
[
"context_file",
[
{
file_name: "file/name.txt",
file_content: "hello",
line1: 1,
line2: 2,
usefulness: 100,
},
],
],
[
"assistant",
"The Mars Rover Kata is a coding exercise that simulates the movement of a rover on the surface of Mars. The goal is to write a program that takes in a series of commands and outputs the final position of the rover.\n\nHere is a link to the official Mars Rover Kata documentation: https://kata-log.rocks/mars-rover-kata\n\nYou can find various implementations and solutions to the Mars Rover Kata on platforms like GitHub or coding challenge websites.",
Expand Down
62 changes: 62 additions & 0 deletions src/features/Chat.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CreateNewChatThread,
ChatErrorStreaming,
ChatReceiveCapsError,
ResponseToChat,
} from "../events";
import {
MARS_ROVER_CHAT,
Expand Down Expand Up @@ -386,4 +387,65 @@ describe("Chat", () => {
"*",
);
});

test("restore and receive response with use question", async () => {
vi.mock("uuid", () => ({ v4: () => "foo" }));
const { user: _user, ...app } = render(<Chat />);

const restoreChatAction: RestoreChat = {
type: EVENT_NAMES_TO_CHAT.RESTORE_CHAT,
payload: {
id: "foo",
chat: {
id: "bar",
messages: [
["user", "/shorter"],
["assistant", "hello there"],
["user", "even shorter still"],
],
title: "hello",
model: "gpt-3.5-turbo",
},
},
};

postMessage(restoreChatAction);

await waitFor(() => expect(app.queryByText("hello there")).not.toBeNull());

const file: ResponseToChat = {
type: EVENT_NAMES_TO_CHAT.CHAT_RESPONSE,
payload: {
id: "bar",
content:
'[{"file_name":"/refact-chat-js/src/services/refact.ts","file_content":"hello","line1":121,"line2":451,"usefulness":100.0}]',
role: "context_file",
},
};

postMessage(file);

const assistant: ResponseToChat = {
type: EVENT_NAMES_TO_CHAT.CHAT_RESPONSE,
payload: {
id: "bar",
role: "user",
content: "even shorter still",
},
};

postMessage(assistant);

postMessage({
type: EVENT_NAMES_TO_CHAT.DONE_STREAMING,
payload: { id: "bar" },
});

await new Promise((r) => setTimeout(r, 500));

const messages = app.getAllByText("even shorter still");
expect(messages.length).toBe(1);

expect(() => app.queryByText("hello there")).not.toBeNull();
});
});
7 changes: 6 additions & 1 deletion src/hooks/useEventBusForChat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ export function reducer(postMessage: typeof window.postMessage) {
},
);

const lastAssistantMessage = messages.reduce((count, message, index) => {
if (message[0] === "assistant") return index + 1;
return count;
}, 0);

return {
...state,
waiting_for_response: false,
streaming: false,
error: null,
previous_message_length: messages.length,
previous_message_length: lastAssistantMessage,
chat: {
...action.payload.chat,
messages,
Expand Down

0 comments on commit 987aa7e

Please sign in to comment.