Skip to content

Commit

Permalink
Add PR #292 (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
davorrunje authored Apr 8, 2024
2 parents 6193e78 + 8a96994 commit 8c91cdc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/main.wasp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ entity Chat {=psl
user User? @relation(fields: [userId], references: [id])
userId Int?
name String? @default("New chat")
isChatNameUpdated Boolean @default(false)
conversations Conversation[]
psl=}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Chat" ADD COLUMN "isChatNameUpdated" BOOLEAN NOT NULL DEFAULT false;
2 changes: 1 addition & 1 deletion app/src/client/admin/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const Sidebar = ({ sidebarOpen, setSidebarOpen }: SidebarProps) => {
return (
<aside
ref={sidebar}
className={`absolute left-0 top-0 z-9999 flex h-screen w-72.5 flex-col overflow-y-hidden bg-captn-dark-blue duration-300 ease-linear dark:bg-captn-dark-blue lg:static lg:translate-x-0 ${
className={`absolute left-0 top-0 z-9999 flex h-screen w-75 flex-col overflow-y-hidden bg-captn-dark-blue duration-300 ease-linear dark:bg-captn-dark-blue lg:static lg:translate-x-0 ${
sidebarOpen ? 'translate-x-0' : '-translate-x-full'
}`}
>
Expand Down
11 changes: 10 additions & 1 deletion app/src/client/app/ChatPage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react';
import { useSocket, useSocketListener } from 'wasp/client/webSocket';
import { type User } from 'wasp/entities';

Expand All @@ -23,8 +24,10 @@ import {
callOpenAiAgent,
handleChatError,
} from '../utils/chatUtils';
import { set } from 'zod';

const ChatPage = ({ user }: { user: User }) => {
const [refetchAllChatDetails, setRefetchAllChatDetails] = useState(false);
const { socket } = useSocket();
const location = useLocation();
const { pathname } = location;
Expand Down Expand Up @@ -66,6 +69,10 @@ const ChatPage = ({ user }: { user: User }) => {
});
};

const refetchChatDetails = () => {
setRefetchAllChatDetails(!refetchAllChatDetails);
};

const handleFormSubmit = async (
userQuery: string,
isUserRespondedWithNextAction: boolean = false,
Expand Down Expand Up @@ -110,7 +117,8 @@ const ChatPage = ({ user }: { user: User }) => {
currentChatDetails,
inProgressConversation,
socket,
messages
messages,
refetchChatDetails
);
}
} catch (err: any) {
Expand Down Expand Up @@ -158,6 +166,7 @@ const ChatPage = ({ user }: { user: User }) => {
handleFormSubmit={handleFormSubmit}
currentChatDetails={currentChatDetails}
triggerChatFormSubmitMsg={triggerChatFormSubmitMsg}
refetchAllChatDetails={refetchAllChatDetails}
>
<div className='flex h-full flex-col'>
{currentChatDetails ? (
Expand Down
3 changes: 3 additions & 0 deletions app/src/client/app/layout/ChatLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ interface Props {
handleFormSubmit: any;
currentChatDetails?: Chat | null;
triggerChatFormSubmitMsg?: string | null;
refetchAllChatDetails: boolean;
}

const ChatLayout: FC<Props> = ({
children,
handleFormSubmit,
currentChatDetails,
triggerChatFormSubmitMsg,
refetchAllChatDetails,
}) => {
const [sidebarOpen, setSidebarOpen] = useState(false);
const { data: user } = useAuth();
Expand Down Expand Up @@ -66,6 +68,7 @@ const ChatLayout: FC<Props> = ({
<ChatSidebar
sidebarOpen={sidebarOpen}
setSidebarOpen={setSidebarOpen}
refetchAllChatDetails={refetchAllChatDetails}
/>
{/* <!-- ===== Sidebar End ===== --> */}

Expand Down
22 changes: 18 additions & 4 deletions app/src/client/components/ChatSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import { updateCurrentChat } from 'wasp/client/operations';
interface ChatSidebarProps {
sidebarOpen: boolean;
setSidebarOpen: (arg: boolean) => void;
refetchAllChatDetails: boolean;
}

const ChatSidebar = ({ sidebarOpen, setSidebarOpen }: ChatSidebarProps) => {
const ChatSidebar = ({
sidebarOpen,
setSidebarOpen,
refetchAllChatDetails,
}: ChatSidebarProps) => {
const history = useHistory();
const location = useLocation();
const { pathname } = location;
Expand All @@ -33,14 +38,23 @@ const ChatSidebar = ({ sidebarOpen, setSidebarOpen }: ChatSidebarProps) => {
id: chatId,
data: {
name: newChatName,
isChatNameUpdated: true,
},
});
} catch (err: any) {
console.log('Unable to update the chat name. Please try again later.');
}
};

const { data: chats, isLoading: isLoadingChats } = useQuery(getChats);
const {
data: chats,
isLoading: isLoadingChats,
refetch: refetchChats,
} = useQuery(getChats);

useEffect(() => {
refetchChats();
}, [refetchAllChatDetails]);

// close on click outside
useEffect(() => {
Expand Down Expand Up @@ -96,7 +110,7 @@ const ChatSidebar = ({ sidebarOpen, setSidebarOpen }: ChatSidebarProps) => {
return (
<aside
ref={sidebar}
className={`border-r-2 absolute left-0 top-0 z-9999 flex h-screen w-72.5 flex-col overflow-y-hidden bg-captn-dark-blue duration-300 ease-linear dark:bg-captn-dark-blue lg:static lg:translate-x-0 ${
className={`border-r-2 absolute left-0 top-0 z-9999 flex h-screen w-75 flex-col overflow-y-hidden bg-captn-dark-blue duration-300 ease-linear dark:bg-captn-dark-blue lg:static lg:translate-x-0 ${
sidebarOpen ? 'translate-x-0' : '-translate-x-full'
}`}
>
Expand Down Expand Up @@ -201,7 +215,7 @@ const ChatSidebar = ({ sidebarOpen, setSidebarOpen }: ChatSidebarProps) => {
>
<path d='M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z'></path>
</svg>
<span className='ml-3'>
<span>
<EditableChatName
chatId={chat.id}
chatName={chat.name ? chat.name : ''}
Expand Down
8 changes: 6 additions & 2 deletions app/src/client/components/EditableChatName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const EditableChatName: React.FC<EditableChatNameProps> = ({
}
}, [isEditing]);

useEffect(() => {
setInputValue(chatName);
}, [chatName]);

const handleIconClick = (event: React.MouseEvent) => {
event.preventDefault();
setIsEditing(true);
Expand All @@ -44,7 +48,7 @@ const EditableChatName: React.FC<EditableChatNameProps> = ({
};

return (
<div className='editable-chat-name' style={{ width: '170px' }}>
<div className='editable-chat-name' style={{ width: '195px' }}>
{!isEditing && (
<span
className='chat-name overflow-ellipsis overflow-hidden whitespace-nowrap'
Expand Down Expand Up @@ -72,7 +76,7 @@ const EditableChatName: React.FC<EditableChatNameProps> = ({
)}
{!isEditing && (
<button
className='edit-button absolute right-3 top-3 text-xs'
className='edit-button absolute right-3 top-3 text-sm'
onClick={handleIconClick}
data-testid='edit-button'
>
Expand Down
16 changes: 12 additions & 4 deletions app/src/client/tests/chatUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,16 @@ describe('chatUtils', () => {
const inProgressConversation = { id: 1 };
const messages: any[] = [];
const activeChatId = 2;
const refetchChatDetails = vi.fn();

await handleAgentResponse(
response,
currentChatDetails,
inProgressConversation,
socket,
messages,
activeChatId
activeChatId,
refetchChatDetails
);

expect(socket.emit).toHaveBeenCalledWith(
Expand Down Expand Up @@ -306,14 +308,16 @@ describe('chatUtils', () => {
const inProgressConversation = { id: 1 };
const messages: any[] = [];
const activeChatId = 2;
const refetchChatDetails = vi.fn();

await handleAgentResponse(
response,
currentChatDetails,
inProgressConversation,
socket,
messages,
activeChatId
activeChatId,
refetchChatDetails
);

expect(socket.emit).toHaveBeenCalledWith(
Expand Down Expand Up @@ -364,14 +368,16 @@ describe('chatUtils', () => {
const inProgressConversation = { id: 1 };
const messages: any = [];
const activeChatId = 2;
const refetchChatDetails = vi.fn();

await handleAgentResponse(
response,
currentChatDetails,
inProgressConversation,
socket,
messages,
activeChatId
activeChatId,
refetchChatDetails
);

expect(operations.updateCurrentConversation).toHaveBeenCalledWith({
Expand Down Expand Up @@ -409,14 +415,16 @@ describe('chatUtils', () => {
const inProgressConversation = { id: 1 };
const messages: any = [];
const activeChatId = 2;
const refetchChatDetails = vi.fn();

await handleAgentResponse(
response,
currentChatDetails,
inProgressConversation,
socket,
messages,
activeChatId
activeChatId,
refetchChatDetails
);

expect(operations.updateCurrentChat).toHaveBeenCalledWith({
Expand Down
21 changes: 18 additions & 3 deletions app/src/client/utils/chatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export const callOpenAiAgent = async (
currentChatDetails: any,
inProgressConversation: any,
socket: any,
messages: any
messages: any,
refetchChatDetails: () => void
) => {
const response = await getAgentResponse({
chatId: activeChatId,
Expand All @@ -128,7 +129,8 @@ export const callOpenAiAgent = async (
inProgressConversation,
socket,
messages,
activeChatId
activeChatId,
refetchChatDetails
);
};

Expand All @@ -138,7 +140,8 @@ export const handleAgentResponse = async (
inProgressConversation: any,
socket: any,
messages: any,
activeChatId: number
activeChatId: number,
refetchChatDetails: () => void
) => {
if (!!response.customer_brief) {
socket.emit(
Expand Down Expand Up @@ -172,6 +175,12 @@ export const handleAgentResponse = async (
},
}));

const chatName = currentChatDetails.isChatNameUpdated
? null
: response['conversation_name']
? response['conversation_name']
: null;

await updateCurrentChat({
id: activeChatId,
data: {
Expand All @@ -182,8 +191,14 @@ export const handleAgentResponse = async (
smartSuggestions: response['smart_suggestions'],
isExceptionOccured: response['is_exception_occured'] || false,
customerBrief: response['customer_brief'],
...(chatName && {
name: chatName,
isChatNameUpdated: true,
}),
},
});

chatName && refetchChatDetails();
};

export const handleChatError = async (
Expand Down
3 changes: 3 additions & 0 deletions app/src/server/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ export const getAgentResponse: GetAgentResponse<
...(json['customer_brief'] !== undefined && {
customer_brief: json['customer_brief'],
}),
...(json['conversation_name'] !== undefined && {
conversation_name: json['conversation_name'],
}),
...(json['is_exception_occured'] !== undefined && {
is_exception_occured: Boolean(json['is_exception_occured']),
}),
Expand Down

0 comments on commit 8c91cdc

Please sign in to comment.