-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
ede880a
initial commit
2c1362a
Merge branch 'main' into refactor-chat
900dd30
update tests and feat-markdown
d8d6aaf
rename
b5ea875
introduce chat
d8ee7e8
initial commit
d538f13
extend chatinterface, add
09a8083
improve styles, code markdown, and adjust styles
1b01ff1
Merge branch 'main' into refactor-chat
9114a7c
update actions to use new reducers
9ac6fbf
Merge branch 'main' into refactor-chat
407170a
Merge branch 'main' into refactor-chat
73c6538
scroll down for new messages
611afa6
Merge branch 'main' into refactor-chat
d23d662
Merge branch 'main' into refactor-chat
4bc1f4e
add fade and action banner
f754971
Merge branch 'main' into refactor-chat
2f8be13
Merge branch 'main' into refactor-chat
amanape 90eae8a
Merge remote-tracking branch 'upstream/main'
2e6afdf
support with jupyter
7df3cd3
refactor to pass tests
c7d2f38
remove unused import
47b2071
Merge remote-tracking branch 'upstream/main'
1086cd1
Merge remote-tracking branch 'upstream/main'
e5a38f5
Merge remote-tracking branch 'upstream/main'
238ea1e
Merge remote-tracking branch 'upstream/main'
18222d7
Merge branch 'main' into refactor-chat
5e7b311
remove outdated files/folders
b29a5b5
Merge branch 'main' into refactor-chat
7ef5db6
Merge branch 'main' into refactor-chat
ee15928
create simple useTyping hook
3c34267
Merge remote-tracking branch 'upstream/main' into refactor-chat
9619d9f
Merge branch 'main' into refactor-chat
fdf7f5a
remove action banner, extend tests, move files
6c8bef8
extend tests
62faad6
disable error
cbd4d54
Merge branch 'main' into refactor-chat
amanape File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
|
||
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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chst.mp4
There was a problem hiding this comment.
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?