Show Khoj agent in agent dropdown by default on mobile in web app home

Previously on slow connection you'd see the agent dropdown flicker
from undefined to Khoj default agent on phones and other thin screens.

This is unnecessary and jarring. Populate with default agent to remove
this issue
This commit is contained in:
Debanjum
2025-03-20 12:24:11 +05:30
parent 9a0b126f12
commit f15a95dccf

View File

@@ -3,7 +3,7 @@ import "./globals.css";
import styles from "./page.module.css"; import styles from "./page.module.css";
import "katex/dist/katex.min.css"; import "katex/dist/katex.min.css";
import React, { useEffect, useRef, useState, useMemo } from "react"; import React, { useEffect, useRef, useState } from "react";
import useSWR from "swr"; import useSWR from "swr";
import { ArrowsVertical } from "@phosphor-icons/react"; import { ArrowsVertical } from "@phosphor-icons/react";
@@ -48,9 +48,19 @@ import { SidebarInset, SidebarProvider, SidebarTrigger } from "@/components/ui/s
import { AppSidebar } from "./components/appSidebar/appSidebar"; import { AppSidebar } from "./components/appSidebar/appSidebar";
import { Separator } from "@/components/ui/separator"; import { Separator } from "@/components/ui/separator";
import { KhojLogoType } from "./components/logo/khojLogo"; import { KhojLogoType } from "./components/logo/khojLogo";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
const fetcher = (url: string) =>
fetch(url)
.then((res) => res.json())
.catch((err) => console.warn(err));
interface ChatBodyDataProps { interface ChatBodyDataProps {
chatOptionsData: ChatOptions | null; chatOptionsData: ChatOptions | null;
onConversationIdChange?: (conversationId: string) => void; onConversationIdChange?: (conversationId: string) => void;
@@ -104,10 +114,11 @@ function AgentCards({
> >
<PopoverTrigger asChild> <PopoverTrigger asChild>
<Card <Card
className={`${selectedAgent === agent.slug className={`${
selectedAgent === agent.slug
? convertColorToBorderClass(agent.color) ? convertColorToBorderClass(agent.color)
: "border-stone-100 dark:border-neutral-700 text-muted-foreground" : "border-stone-100 dark:border-neutral-700 text-muted-foreground"
} }
hover:cursor-pointer rounded-lg px-2 py-2`} hover:cursor-pointer rounded-lg px-2 py-2`}
onDoubleClick={() => openAgentEditCard(agent.slug)} onDoubleClick={() => openAgentEditCard(agent.slug)}
onClick={() => { onClick={() => {
@@ -144,7 +155,7 @@ function AgentCards({
selectedChatModelOption="" selectedChatModelOption=""
agentSlug="" agentSlug=""
isSubscribed={isUserSubscribed(userConfig)} isSubscribed={isUserSubscribed(userConfig)}
setAgentChangeTriggered={() => { }} setAgentChangeTriggered={() => {}}
modelOptions={[]} modelOptions={[]}
inputToolOptions={{}} inputToolOptions={{}}
outputModeOptions={{}} outputModeOptions={{}}
@@ -199,12 +210,7 @@ function ChatBodyData(props: ChatBodyDataProps) {
const onConversationIdChange = props.onConversationIdChange; const onConversationIdChange = props.onConversationIdChange;
const agentsFetcher = () => const { data: agentsData, error } = useSWR<AgentData[]>("/api/agents", fetcher, {
window
.fetch("/api/agents")
.then((res) => res.json())
.catch((err) => console.log(err));
const { data: agentsData, error } = useSWR<AgentData[]>("agents", agentsFetcher, {
revalidateOnFocus: false, revalidateOnFocus: false,
}); });
@@ -222,8 +228,8 @@ function ChatBodyData(props: ChatBodyDataProps) {
today.getHours() >= 17 || today.getHours() < 4 today.getHours() >= 17 || today.getHours() < 4
? "evening" ? "evening"
: today.getHours() >= 12 : today.getHours() >= 12
? "afternoon" ? "afternoon"
: "morning"; : "morning";
const nameSuffix = props.userConfig?.given_name ? `, ${props.userConfig?.given_name}` : ""; const nameSuffix = props.userConfig?.given_name ? `, ${props.userConfig?.given_name}` : "";
const greetings = [ const greetings = [
`What would you like to get done${nameSuffix}?`, `What would you like to get done${nameSuffix}?`,
@@ -354,7 +360,7 @@ function ChatBodyData(props: ChatBodyDataProps) {
setUploadedFiles={props.setUploadedFiles} setUploadedFiles={props.setUploadedFiles}
agentColor={agents.find((agent) => agent.slug === selectedAgent)?.color} agentColor={agents.find((agent) => agent.slug === selectedAgent)?.color}
ref={chatInputRef} ref={chatInputRef}
setTriggeredAbort={() => { }} setTriggeredAbort={() => {}}
/> />
</div> </div>
)} )}
@@ -438,21 +444,23 @@ function ChatBodyData(props: ChatBodyDataProps) {
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger> <DropdownMenuTrigger>
<Button className="w-full m-2 p-0" variant="outline"> <Button className="w-full m-2 p-0" variant="outline">
{ {selectedAgent ? (
selectedAgent ? ( getIconFromIconName(
getIconFromIconName( agents.find((agent) => agent.slug === selectedAgent)
agents.find((agent) => agent.slug === selectedAgent)?.icon ?? "", ?.icon ?? "Lightbulb",
agents.find((agent) => agent.slug === selectedAgent)?.color ?? "" agents.find((agent) => agent.slug === selectedAgent)
) ?.color ?? "orange",
) : (
<ArrowsVertical className="h-5 w-5" />
) )
} ) : (
{selectedAgent ? `${agents.find((agent) => agent.slug === selectedAgent)?.name}` : "Select Agent"} <ArrowsVertical className="h-5 w-5" />
)}
{selectedAgent
? `${agents?.find((agent) => agent.slug === selectedAgent)?.name ?? "Khoj"}`
: "Select Agent"}
</Button> </Button>
</DropdownMenuTrigger> </DropdownMenuTrigger>
<DropdownMenuContent align="end" className="overflow-y-scroll h-96"> <DropdownMenuContent align="end" className="overflow-y-scroll max-h-96">
{ {agentIcons.length > 0 ? (
agentIcons.map((icon, index) => ( agentIcons.map((icon, index) => (
<DropdownMenuItem <DropdownMenuItem
key={`${index}-${agents[index].slug}`} key={`${index}-${agents[index].slug}`}
@@ -464,7 +472,17 @@ function ChatBodyData(props: ChatBodyDataProps) {
{icon} {agents[index].name} {icon} {agents[index].name}
</DropdownMenuItem> </DropdownMenuItem>
)) ))
} ) : (
<DropdownMenuItem
key="0-khoj"
onClick={() => {
setSelectedAgent("khoj");
chatInputRef.current?.focus();
}}
>
{getIconFromIconName("Lightbulb", "orange")} Khoj
</DropdownMenuItem>
)}
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
<div <div
@@ -473,15 +491,19 @@ function ChatBodyData(props: ChatBodyDataProps) {
<ChatInputArea <ChatInputArea
isLoggedIn={props.isLoggedIn} isLoggedIn={props.isLoggedIn}
sendMessage={(message) => setMessage(message)} sendMessage={(message) => setMessage(message)}
sendImage={(image) => setImages((prevImages) => [...prevImages, image])} sendImage={(image) =>
setImages((prevImages) => [...prevImages, image])
}
sendDisabled={processingMessage} sendDisabled={processingMessage}
chatOptionsData={props.chatOptionsData} chatOptionsData={props.chatOptionsData}
conversationId={null} conversationId={null}
isMobileWidth={props.isMobileWidth} isMobileWidth={props.isMobileWidth}
setUploadedFiles={props.setUploadedFiles} setUploadedFiles={props.setUploadedFiles}
agentColor={agents.find((agent) => agent.slug === selectedAgent)?.color} agentColor={
agents.find((agent) => agent.slug === selectedAgent)?.color
}
ref={chatInputRef} ref={chatInputRef}
setTriggeredAbort={() => { }} setTriggeredAbort={() => {}}
/> />
</div> </div>
</div> </div>