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

feat: add chat interaction UI #152

Merged
merged 9 commits into from
Jun 15, 2023

Conversation

cguedes
Copy link
Collaborator

@cguedes cguedes commented Jun 13, 2023

This PR closes #148 by adding a UI for chat interaction.

Note that currently the backend don't support threads and history (will be fixed by #144) so this PR adds that as a UI state in order to experiment the interaction pattern.

image

@cguedes cguedes linked an issue Jun 13, 2023 that may be closed by this pull request
src/index.css Show resolved Hide resolved
@cguedes cguedes marked this pull request as ready for review June 13, 2023 15:36
@cguedes cguedes requested a review from sehyod June 13, 2023 15:36
@cguedes
Copy link
Collaborator Author

cguedes commented Jun 13, 2023

This is ready for review @sehyod

Copy link
Collaborator

@sehyod sehyod left a comment

Choose a reason for hiding this comment

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

Thank you for this PR and for the work to add custom tailwind components!

src/panels/ai/ChatPanelSection.tsx Outdated Show resolved Hide resolved
src/panels/ai/ChatPanelSection.tsx Outdated Show resolved Hide resolved
@cguedes cguedes requested a review from sehyod June 14, 2023 08:30
@hammer
Copy link
Contributor

hammer commented Jun 14, 2023

Very exciting! We're getting close to the MLP.

I hit an error when I tried to chat before uploading any references:

Chat error: Error: Traceback (most recent call last): File "main.py", line 13, in <module> File "sidecar/chat.py", line 30, in ask_question File "sidecar/storage.py", line 14, in load FileNotFoundError: [Errno 2] No such file or directory: '/Users/gium/Library/Application Support/com.tauri.dev/project-x/.storage/references.json' [50699] Failed to execute script 'main' due to unhandled exception!

I was able to get this working with a PDF I uploaded though, very cool!
Screenshot 2023-06-14 at 10 53 32 AM

@cguedes
Copy link
Collaborator Author

cguedes commented Jun 14, 2023

That's the expected behaviour if you didn't uploaded PDFs. I forgot to mention that in the PR description.

@cguedes cguedes requested a review from sehyod June 14, 2023 09:42
sehyod
sehyod previously approved these changes Jun 14, 2023
@hammer
Copy link
Contributor

hammer commented Jun 14, 2023

That's the expected behaviour if you didn't uploaded PDFs.

Rather than the unfriendly error message can we say something more useful about needing to upload PDFs to chat? It's not clear to me that's even the right behavior. We should probably just mention to the user they can upload PDFs to improve the chat experience and pass through their chat to get a response.

@cguedes
Copy link
Collaborator Author

cguedes commented Jun 14, 2023

That's a good point Jeff. In the frontend we can't distinguish between errors in the backend. You can have this one but also an API Rate limit, or invalid API Key, ..

I think that we should add (reply error codes) to the #144 and then improve the UI on top of that.

@hammer
Copy link
Contributor

hammer commented Jun 14, 2023

In the frontend we can't distinguish between errors in the backend.

Ah, perhaps we should file a broader issue for better sharing of error state between the frontend and backend?

@cguedes cguedes requested a review from sehyod June 15, 2023 09:31
@sergioramos sergioramos changed the title feat: Adds chat interaction UI feat: add chat interaction UI Jun 15, 2023
@sergioramos sergioramos merged commit cd15dc7 into main Jun 15, 2023
9 checks passed
@sergioramos sergioramos deleted the 148-include-chat-in-ai-interaction-component branch June 15, 2023 09:54
Copy link
Collaborator

@danvk danvk left a comment

Choose a reason for hiding this comment

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

A few more comments. I got this to work in my local client.


export async function askForRewrite(selection: string): Promise<string> {
try {
const response = await callSidecar('rewrite', ['--text', String(selection)]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The String() wrapper here isn't necessary (if the TS types are to be believed!).

// 'Hidden feedback loops in machine learning refer to situations where two systems indirectly influence each other through the world, leading to changes in behavior that may not be immediately visible. These loops may exist between completely disjoint systems and can make analyzing the effect of proposed changes extremely difficult, adding cost to even simple improvements. It is recommended to look carefully for hidden feedback loops and remove them whenever feasible.',
// },
]);
const [currentChatThreadItem, setCurrentChatThreadItem] = useState<ChatThreadItem | null>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want to move the thread state into atoms? Presumably the interactions will grow more elaborate in the future and we'll want it to persist even if you close the component. Alternatively the thread state could live in the backend.

]);
const [currentChatThreadItem, setCurrentChatThreadItem] = useState<ChatThreadItem | null>(null);

function handleKeyDown(evt: React.KeyboardEvent<HTMLTextAreaElement>): void {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: more concisely written as:

const handleKeydown: React.KeyboardEventHandler = (evt) => {

}
setCurrentChatThreadItem({ id: String(chatThread.length + 1), question: text });
setText('');
chatWithAI(question)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest using async / await.

setText('');
chatWithAI(question)
.then((reply) => {
setChatThread([...chatThread, { id: String(chatThread.length + 1), question: text, answer: reply[0] }]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

There may be a stale closure issue here if you send two chats before the first one comes back. Safer to use the function form of setState:

setChatThread(prevChatThread => [...prevChatThread, { id: String(prevChatThread.length + 1), question: text, answer: reply[0] }]);

(also below)

expect(screen.getByText('Will this test pass?')).toBeInTheDocument();
expect(screen.getByText('...')).toBeInTheDocument();

resolveFn();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice test!

@@ -2,26 +2,26 @@ import { render, screen, userEvent } from '../utils/test-utils';
import { PrimarySideBar } from './PrimarySideBar';

describe('PrimarySideBar', () => {
it('should display Explorer and References icons', () => {
test('should display Explorer and References icons', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I left a comment about this elsewhere, but when we write "should" in a test description we should use it rather than test to make it read more clearly (I pretty much always use it) https://stackoverflow.com/questions/45778192/what-is-the-difference-between-it-and-test-in-jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include chat in AI interaction component
5 participants