Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor|test)(frontend): chat #1488

Merged
merged 37 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ede880a
initial commit
Apr 29, 2024
2c1362a
Merge branch 'main' into refactor-chat
May 1, 2024
900dd30
update tests and feat-markdown
May 1, 2024
d8d6aaf
rename
May 1, 2024
b5ea875
introduce chat
May 1, 2024
d8ee7e8
initial commit
May 1, 2024
d538f13
extend chatinterface, add
May 1, 2024
09a8083
improve styles, code markdown, and adjust styles
May 1, 2024
1b01ff1
Merge branch 'main' into refactor-chat
May 1, 2024
9114a7c
update actions to use new reducers
May 1, 2024
9ac6fbf
Merge branch 'main' into refactor-chat
May 1, 2024
407170a
Merge branch 'main' into refactor-chat
May 2, 2024
73c6538
scroll down for new messages
May 2, 2024
611afa6
Merge branch 'main' into refactor-chat
May 2, 2024
d23d662
Merge branch 'main' into refactor-chat
May 2, 2024
4bc1f4e
add fade and action banner
May 2, 2024
f754971
Merge branch 'main' into refactor-chat
May 2, 2024
2f8be13
Merge branch 'main' into refactor-chat
amanape May 2, 2024
90eae8a
Merge remote-tracking branch 'upstream/main'
May 2, 2024
2e6afdf
support with jupyter
May 2, 2024
7df3cd3
refactor to pass tests
May 2, 2024
c7d2f38
remove unused import
May 2, 2024
47b2071
Merge remote-tracking branch 'upstream/main'
May 2, 2024
1086cd1
Merge remote-tracking branch 'upstream/main'
May 3, 2024
e5a38f5
Merge remote-tracking branch 'upstream/main'
May 3, 2024
238ea1e
Merge remote-tracking branch 'upstream/main'
May 3, 2024
18222d7
Merge branch 'main' into refactor-chat
May 3, 2024
5e7b311
remove outdated files/folders
May 3, 2024
b29a5b5
Merge branch 'main' into refactor-chat
May 3, 2024
7ef5db6
Merge branch 'main' into refactor-chat
May 3, 2024
ee15928
create simple useTyping hook
May 3, 2024
3c34267
Merge remote-tracking branch 'upstream/main' into refactor-chat
May 3, 2024
9619d9f
Merge branch 'main' into refactor-chat
May 4, 2024
fdf7f5a
remove action banner, extend tests, move files
May 4, 2024
6c8bef8
extend tests
May 4, 2024
62faad6
disable error
May 4, 2024
cbd4d54
Merge branch 'main' into refactor-chat
amanape May 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useDisclosure } from "@nextui-org/react";
import React, { useEffect, useState } from "react";
import { Toaster } from "react-hot-toast";
import CogTooth from "#/assets/cog-tooth";
import ChatInterface from "#/components/ChatInterface";
import ChatInterface from "#/components/chat/ChatInterface";
import Errors from "#/components/Errors";
import { Container, Orientation } from "#/components/Resizable";
import Workspace from "#/components/Workspace";
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/AgentControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import PauseIcon from "#/assets/pause";
import PlayIcon from "#/assets/play";
import { changeTaskState } from "#/services/agentStateService";
import { clearMsgs } from "#/services/session";
import { clearMessages } from "#/state/chatSlice";
import store, { RootState } from "#/store";
import AgentTaskAction from "#/types/AgentTaskAction";
import AgentTaskState from "#/types/AgentTaskState";
import { clearMessages } from "#/state/chatSlice";

const TaskStateActionMap = {
[AgentTaskAction.START]: AgentTaskState.RUNNING,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function ChatInput({ disabled, onSendMessage }: ChatInputProps) {
onCompositionStart={() => setIsComposing(true)}
onCompositionEnd={() => setIsComposing(false)}
placeholder={t(I18nKey.CHAT_INTERFACE$INPUT_PLACEHOLDER)}
className="pt-2 pb-3 px-3"
className="pb-3 px-3"
classNames={{
inputWrapper: "bg-neutral-700 border border-neutral-600 rounded-lg",
input: "pr-16 text-neutral-400",
Expand Down
140 changes: 0 additions & 140 deletions frontend/src/components/ChatInterface.tsx

This file was deleted.

37 changes: 37 additions & 0 deletions frontend/src/components/chat/Chat.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import { act, render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import Chat from "./Chat";

const MESSAGES: Message[] = [
{ sender: "assistant", content: "Hello!" },
{ sender: "user", content: "Hi!" },
{ sender: "assistant", content: "How can I help you today?" },
];

HTMLElement.prototype.scrollIntoView = vi.fn();

describe("Chat", () => {
it("should render chat messages", () => {
render(<Chat messages={MESSAGES} />);

const messages = screen.getAllByTestId("message");

expect(messages).toHaveLength(MESSAGES.length);
});

it("should scroll to the newest message", () => {
const { rerender } = render(<Chat messages={MESSAGES} />);

const newMessages: Message[] = [
...MESSAGES,
{ sender: "user", content: "Create a spaceship" },
];

act(() => {
rerender(<Chat messages={newMessages} />);
});

expect(HTMLElement.prototype.scrollIntoView).toHaveBeenCalled();
});
});
25 changes: 25 additions & 0 deletions frontend/src/components/chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import ChatMessage from "./ChatMessage";

interface ChatProps {
messages: Message[];
}

function Chat({ messages }: ChatProps) {
const endOfMessagesRef = React.useRef<HTMLDivElement>(null);

React.useEffect(() => {
endOfMessagesRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages]);

return (
<div className="flex flex-col gap-3">
{messages.map((message, index) => (
<ChatMessage key={index} message={message} />
))}
<div ref={endOfMessagesRef} />
</div>
);
}

export default Chat;
91 changes: 91 additions & 0 deletions frontend/src/components/chat/ChatInterface.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from "react";
import { screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import { act } from "react-dom/test-utils";
import userEvent from "@testing-library/user-event";
import { renderWithProviders } from "test-utils";
import ChatInterface from "./ChatInterface";
import Socket from "#/services/socket";
import ActionType from "#/types/ActionType";
import { addAssistantMessage } from "#/state/chatSlice";
import AgentTaskState from "#/types/AgentTaskState";
import { changeTaskState } from "#/state/agentSlice";

// avoid typing side-effect
vi.mock("#/hooks/useTyping", () => ({
useTyping: vi.fn((text: string) => text),
}));

const socketSpy = vi.spyOn(Socket, "send");

// This is for the scrollview ref in Chat.tsx
// TODO: Move this into test setup
HTMLElement.prototype.scrollIntoView = vi.fn();

describe("ChatInterface", () => {
it("should render the messages and input", () => {
renderWithProviders(<ChatInterface />);
expect(screen.queryAllByTestId("message")).toHaveLength(1); // initial welcome message only
});

it("should render the new message the user has typed", async () => {
renderWithProviders(<ChatInterface />);

const input = screen.getByRole("textbox");

act(() => {
userEvent.type(input, "my message{enter}");
});

expect(screen.getByText("my message")).toBeInTheDocument();
});

it("should render user and assistant messages", () => {
const { store } = renderWithProviders(<ChatInterface />, {
preloadedState: {
chat: {
messages: [{ sender: "user", content: "Hello" }],
},
},
});

expect(screen.getAllByTestId("message")).toHaveLength(1);
expect(screen.getByText("Hello")).toBeInTheDocument();

act(() => {
store.dispatch(addAssistantMessage("Hello to you!"));
});

expect(screen.getAllByTestId("message")).toHaveLength(2);
expect(screen.getByText("Hello to you!")).toBeInTheDocument();
});

it("should send the a user message event to the Socket", () => {
renderWithProviders(<ChatInterface />);
const input = screen.getByRole("textbox");
act(() => {
userEvent.type(input, "my message{enter}");
});

const event = { action: ActionType.START, args: { task: "my message" } };
expect(socketSpy).toHaveBeenCalledWith(JSON.stringify(event));
});

it("should display a typing indicator when waiting for assistant response", () => {
const { store } = renderWithProviders(<ChatInterface />, {
preloadedState: {
chat: {
messages: [{ sender: "assistant", content: "Hello" }],
},
},
});

expect(screen.queryByTestId("typing")).not.toBeInTheDocument();

act(() => {
store.dispatch(changeTaskState(AgentTaskState.RUNNING));
});

expect(screen.getByTestId("typing")).toBeInTheDocument();
});
});
62 changes: 62 additions & 0 deletions frontend/src/components/chat/ChatInterface.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { IoMdChatbubbles } from "react-icons/io";
import ChatInput from "../ChatInput";
import Chat from "./Chat";
import { RootState } from "#/store";
import AgentTaskState from "#/types/AgentTaskState";
import { addUserMessage } from "#/state/chatSlice";
import ActionType from "#/types/ActionType";
import Socket from "#/services/socket";

function ActionBanner() {
return (
<div
data-testid="typing"
className="flex items-center justify-center gap-2 bg-neutral-700 border-y border-neutral-500 py-1.5 px-4"
>
<div className="flex h-5 w-5 items-center justify-center" />
<p className="text-sm text-gray-200 dark:text-gray-200">Working...</p>
</div>
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@openartist can you take a look at this? I like the typing animation better. Now we have the messages popping in with this banner that seems a bit out of place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chst.mp4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a design in the figma for a three dot animation appearing in the bubble before text appears. @Sparkier let me know if you have time to review some options. I agree that even if we aren't streaming the AI response the typewriter effect gives a greater sense of responsiveness—perhaps we can just refine it a bit?


function ChatInterface() {
const { messages } = useSelector((state: RootState) => state.chat);
const { curTaskState } = useSelector((state: RootState) => state.agent);

const dispatch = useDispatch();

const handleSendMessage = (content: string) => {
dispatch(addUserMessage(content));

let event;
if (curTaskState === AgentTaskState.INIT) {
event = { action: ActionType.START, args: { task: content } };
} else {
event = { action: ActionType.USER_MESSAGE, args: { content } };
}

Socket.send(JSON.stringify(event));
};

return (
<div className="flex flex-col h-full bg-neutral-800">
<div className="flex items-center gap-2 border-b border-neutral-600 text-sm px-4 py-2">
<IoMdChatbubbles />
Chat
</div>
<div className="flex-1 flex flex-col relative min-h-0">
<div className="overflow-x-auto p-3">
<Chat messages={messages} />
</div>
{curTaskState === AgentTaskState.RUNNING && <ActionBanner />}
{/* Fade between messages and input */}
<div className="absolute bottom-0 left-0 right-0 h-4 bg-gradient-to-b from-transparent to-neutral-800" />
</div>
<ChatInput onSendMessage={handleSendMessage} />
</div>
);
}

export default ChatInterface;