Skip to content

Commit

Permalink
Feat: Husky feedback api change
Browse files Browse the repository at this point in the history
  • Loading branch information
yosuva-rajendran committed Jan 3, 2025
1 parent 674b9c2 commit 7835bf2
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 81 deletions.
9 changes: 5 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ScrollToTop from '@/components/page/home/featured/scroll-to-top';
import { getFeaturedData } from '@/services/featured.service';

export default async function Home() {
const { featuredData, discoverData, isLoggedIn, isError, userInfo, focusAreas } = await getPageData();
const { featuredData, discoverData, isLoggedIn, isError, userInfo, authToken, focusAreas } = await getPageData();

if (isError) {
return <Error />;
Expand All @@ -39,13 +39,13 @@ export default async function Home() {
<ScrollToTop pageName='Home' userInfo={userInfo}/>
</div>
</div>
<HuskyDialog isLoggedIn={isLoggedIn}/>
<HuskyDiscover isLoggedIn={isLoggedIn}/>
<HuskyDialog isLoggedIn={isLoggedIn} authToken={authToken}/>
<HuskyDiscover isLoggedIn={isLoggedIn} authToken={authToken}/>
</>
}

const getPageData = async () => {
const { isLoggedIn, userInfo } = getCookiesFromHeaders();
const { isLoggedIn, userInfo, authToken } = getCookiesFromHeaders();
let isError = false;
let featuredData = [] as any;
let discoverData = [] as any;
Expand Down Expand Up @@ -80,6 +80,7 @@ const getPageData = async () => {
isError,
userInfo,
isLoggedIn,
authToken,
focusAreas: {
teamFocusAreas ,
projectFocusAreas
Expand Down
77 changes: 45 additions & 32 deletions components/core/husky/husky-ai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface HuskyAiProps {
isLoggedIn: boolean;
blogId?: string;
onClose?: () => void;
authToken: string;
}

interface Chat {
Expand All @@ -41,7 +42,7 @@ const DEFAULT_TAB_ITEMS = [

// This component represents the Husky AI interface, allowing users to interact with the AI in chat or blog modes.

function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose }: HuskyAiProps) {
function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose, authToken }: HuskyAiProps) {
const [activeTab, setActiveTab] = useState<string>(DEFAULT_TAB_ITEMS[0].key);
const [chats, setChats] = useState<Chat[]>(initialChats);
const [isLoading, setLoadingStatus] = useState<boolean>(false);
Expand All @@ -55,36 +56,6 @@ function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose
const chatCnRef = useRef<HTMLDivElement>(null);
const router = useRouter();
const { trackTabSelection, trackUserPrompt, trackAnswerCopy, trackFollowupQuestionClick, trackQuestionEdit, trackRegenerate, trackCopyUrl, trackFeedbackClick, trackAiResponse } = useHuskyAnalytics();

const {
object,
isLoading: isLoadingObject,
submit,
error,
} = experimental_useObject({
api: `${process.env.DIRECTORY_API_URL}/v1/husky/chat/assistant`,
schema: z.object({
content: z.string(),
followUpQuestions: z.array(z.string()),
sources: z.array(z.string()).optional(),
actions: z
.array(
z.object({
name: z.string(),
directoryLink: z.string(),
type: z.string(),
})
)
.optional(),
}),
onFinish: (data) => {
console.log(data);
},
onError: (error) => {
console.error(error);
setAnswerLoadingStatus(false);
}
});

// Handles the selection of a tab in the UI
const onTabSelected = (item: string) => {
Expand Down Expand Up @@ -135,6 +106,41 @@ function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose
};
};


const {
object,
isLoading: isLoadingObject,
submit,
error,
} = experimental_useObject({
api: `${process.env.DIRECTORY_API_URL}/v1/husky/chat/assistant`,
headers: {
"Authorization": `Bearer ${authToken}`,
"Content-Type": "application/json",
},
schema: z.object({
content: z.string(),
followUpQuestions: z.array(z.string()),
sources: z.array(z.string()).optional(),
actions: z
.array(
z.object({
name: z.string(),
directoryLink: z.string(),
type: z.string(),
})
)
.optional(),
}),
onFinish: (data) => {
console.log(data);
},
onError: (error) => {
console.error(error);
setAnswerLoadingStatus(false);
}
});

// Checks and sets the prompt ID for the current chat session
const checkAndSetPromptId = () => {
let chatUid = threadId;
Expand Down Expand Up @@ -229,12 +235,20 @@ function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose
const chatUid = checkAndSetPromptId();
setAskingQuestion(question);
setAnswerLoadingStatus(true);

const previousQues = chats.length === 1 ? chats[0].question : null;
const previousAns = chats.length === 1 ? chats[0].answer : null;

await submit({
uid: chatUid,
question,
...(userInfo?.name && { name: userInfo?.name }),
...(userInfo?.email && { email: userInfo?.email }),
...(userInfo?.uid && { directoryId: userInfo?.uid }),
...( mode === 'blog' && { chatSummary: {
user: previousQues,
system: previousAns
}}),
source: selectedSource,
});
// const result = await getHuskyResponse(userInfo, authToken, question, selectedSource, chatUid, mode === 'blog' && chats.length === 1 ? chats[0].question : null, mode === 'blog' && chats.length === 1 ? chats[0].answer : null, mode === 'blog'); // Fixed function name
Expand Down Expand Up @@ -414,7 +428,6 @@ function HuskyAi({ mode = 'chat', initialChats = [], isLoggedIn, blogId, onClose

{mode === 'blog' && (
<div className="huskyai" data-testid="husky-ai-blog">
sec 2
<div ref={chatCnRef} className="huskyai__cn" data-testid="blog-chat-container">
<HuskyChat
onFeedback={onFeedback}
Expand Down
4 changes: 2 additions & 2 deletions components/core/husky/husky-answer-loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ function HuskyAnswerLoader(props: any) {
return (
<>
<div id="answer-loader" className="husky-loader">
<div className="husky-loader__ques">
{/* <div className="husky-loader__ques">
<h2>{question}</h2>
</div>
</div> */}
<div className="huksy-loader__body">
<div className="husky-loader__sources">
<div className="husky-loader__ans__line"></div>
Expand Down
86 changes: 50 additions & 36 deletions components/core/husky/husky-chat-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use client'
'use client';

import { useHuskyAnalytics } from "@/analytics/husky.analytics";
import { useHuskyAnalytics } from '@/analytics/husky.analytics';

interface HuskyChatActionsProps {
actions: any[];
}
function HuskyChatActions({ actions }: HuskyChatActionsProps) {

const {trackHuskyActionCardClicked} = useHuskyAnalytics()
const { trackHuskyActionCardClicked } = useHuskyAnalytics();
const onActionCardClicked = (action: any) => {
trackHuskyActionCardClicked(action)
}
trackHuskyActionCardClicked(action);
};
return (
<>
<div className="chat-actions">
Expand All @@ -19,37 +18,40 @@ function HuskyChatActions({ actions }: HuskyChatActionsProps) {
<span>Suggested actions</span>
</h3>
<div className="chat-actions__cn ">
{actions.map((action: any) => (
<a target="_blank" href={action.directoryLink} onClick={() => onActionCardClicked(action)} className="chat-actions__cn__item" key={action.directoryLink}>
<div></div>
<div className="center">
<p className="chat-actions__cn__item__name">
<span>{action.name}</span>
</p>
<p className="chat-action-type">{`(${action.type})`}</p>
{actions.map((action: any, index: number) => (
<a target="_blank" href={action.directoryLink} onClick={() => onActionCardClicked(action)} className="chat-actions__cn__item" key={index}>
<div className='chat-actions__cn__item__wrpr'>
{ (action.type)?.toLowerCase() === 'member' && <img className="actions__cn__item__name__icon" src="/icons/default_profile.svg" alt="icon" />}
{ (action.type)?.toLowerCase() === 'team' && <img className="actions__cn__item__name__icon" src="/icons/team-default-profile.svg" alt="icon" />}
{ (action.type)?.toLowerCase() === 'project' && <img className="actions__cn__item__name__icon" src="/icons/default-project.svg" alt="icon" />}
<div className="">
<p className="chat-actions__cn__item__name">
<span>{action.name}</span>
</p>
{action.type && <p className="chat-action-type">{`${action.type}`}</p>}
</div>
</div>
<div></div>


<img height={20} width={20} src="/icons/open-link.svg" alt="arrow" />
</a>
))}
</div>
</div>
<style jsx>
{`
.center {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.chat-action-type {
text-transform: capitalize;
margin-top: 3px;
width: fit-content;
font-size: 14px;
text-transform: capitalize;
margin-top: 3px;
width: fit-content;
color: #64748B;
font-weight: 400;
}
.chat-actions {
width: 100%;
Expand All @@ -67,9 +69,10 @@ function HuskyChatActions({ actions }: HuskyChatActionsProps) {
}
.chat-actions__cn {
width: 100%;
display: flex;
display: grid;
gap: 16px;
flex-wrap: wrap;
// flex-wrap: wrap;
grid-template-columns: repeat(1, minmax(0, 1fr));
margin-top: 16px;
}
.chat-actions__cn__item {
Expand All @@ -78,21 +81,27 @@ function HuskyChatActions({ actions }: HuskyChatActionsProps) {
border-radius: 8px;
width: 100%;
display: flex;
flex-direction: column;
gap: 12px;
cursor: pointer;
justify-content: space-between;
align-items: center;
}
.chat-actions__cn__item__wrpr {
display: flex;
gap: 12px;
align-items: center;
}
.chat-actions__cn__item__name {
font-size: 18px;
font-weight: 600;
font-size: 14px;
font-weight: 400;
display: flex;
gap: 6px;
align-items: center;
}
.actions__cn__item__name__icon {
width: 17px;
height: 17px;
width: 32px;
height: 32px;
margin-bottom: 1px;
border-radius: 50%;
}
Expand All @@ -112,15 +121,20 @@ function HuskyChatActions({ actions }: HuskyChatActionsProps) {
padding: 10px 12px;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
font-weight: 400;
text-align: center;
text-transform: capitalize;
}
@media (min-width: 1024px) {
.chat-actions__cn__item {
width: 304px;
@media (min-width: 475px) {
.chat-actions__cn {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}
@media (min-width: 1024px) {
.chat-actions__cn {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
`}
Expand Down
1 change: 1 addition & 0 deletions components/core/husky/husky-chat-suggestion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function HuskyChatSuggestions({ followupQuestions = [], chatIndex = 0, onFollowu
font-weight: 400;
cursor: pointer;
padding: 8px 14px;
border-radius: 8px;
}
`}
</style>
Expand Down
4 changes: 2 additions & 2 deletions components/core/husky/husky-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function HuskyChat({ mode, chats, onFollowupClicked, isAnswerLoading, onQuestion
shareCount={chat?.shareCount}
question={chat?.question}
/>
<HuskyChatAnswer
{chat.answer && <HuskyChatAnswer
onCopyAnswer={onCopyAnswer}
onFeedback={onFeedback}
onRegenerate={onRegenerate}
Expand All @@ -45,7 +45,7 @@ function HuskyChat({ mode, chats, onFollowupClicked, isAnswerLoading, onQuestion
question={chat?.question}
mode={mode}
answer={chat?.answer}
/>
/>}
{chat?.followupQuestions?.length > 0 && <HuskyChatSuggestions isAnswerLoading={isAnswerLoading} chatIndex={index} onFollowupClicked={onFollowupClicked} followupQuestions={chat?.followupQuestions} />}
{mode !== 'blog' && chat?.actions?.length > 0 && <HuskyChatActions actions={chat?.actions} />}
</>
Expand Down
3 changes: 2 additions & 1 deletion components/core/husky/husky-feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const HuskyFeedback = (props: HuskyFeedbackProps) => {
const payload = {
name: memberDetails.name,
email: memberDetails.email,
team: memberDetails.teamMemberRoles[0].teamTitle,
team: memberDetails.teamMemberRoles[0]?.teamTitle ?? '',
directoryId: memberDetails.uid,
rating: ratingInfo.rating,
comment: ratingInfo.comment,
Expand All @@ -77,6 +77,7 @@ const HuskyFeedback = (props: HuskyFeedbackProps) => {
}
}
} catch (error) {
console.log("errr while send", error)
trackFeedbackStatus('error', ratingInfo.rating.toString(), question);
setStep('error');
}
Expand Down
3 changes: 2 additions & 1 deletion components/page/home/husky-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEffect, useRef, useState } from 'react';
function HuskyDialog(props:any) {
const dialogRef = useRef<HTMLDialogElement>(null);
const isLoggedIn = props?.isLoggedIn;
const authToken = props?.authToken;
const [isOpen, setIsOpen] = useState(false);

const onDialogClose = () => {
Expand Down Expand Up @@ -34,7 +35,7 @@ function HuskyDialog(props:any) {
<img onClick={onDialogClose} className="hd__head__close" src="/icons/close.svg" />
</div>
<div className="hd__content">
{isOpen && <HuskyAi onClose={onDialogClose} isLoggedIn={isLoggedIn} />}
{isOpen && <HuskyAi onClose={onDialogClose} isLoggedIn={isLoggedIn} authToken={authToken} />}
</div>
</dialog>
<style jsx>
Expand Down
Loading

0 comments on commit 7835bf2

Please sign in to comment.