Skip to content

Commit

Permalink
fix: show selected thread in agents threads dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ivyjeong13 committed Dec 30, 2024
1 parent 95281e5 commit 6d38bb7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
4 changes: 3 additions & 1 deletion ui/admin/app/components/agent/Agent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { useDebounce } from "~/hooks/useDebounce";

type AgentProps = {
className?: string;
currentThreadId?: string | null;
onRefresh?: (threadId: string | null) => void;
};

export function Agent({ className, onRefresh }: AgentProps) {
export function Agent({ className, currentThreadId, onRefresh }: AgentProps) {
const { agent, updateAgent, isUpdating, lastUpdated, error } = useAgent();

const [agentUpdates, setAgentUpdates] = useState(agent);
Expand Down Expand Up @@ -179,6 +180,7 @@ export function Agent({ className, onRefresh }: AgentProps) {

<div className="flex gap-2">
<PastThreads
currentThreadId={currentThreadId}
agentId={agent.id}
onThreadSelect={handleThreadSelect}
/>
Expand Down
52 changes: 38 additions & 14 deletions ui/admin/app/components/agent/PastThreads.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChevronUpIcon } from "lucide-react";
import { CheckIcon, ChevronUpIcon } from "lucide-react";
import React, { useState } from "react";
import useSWR from "swr";

Expand Down Expand Up @@ -29,11 +29,13 @@ import {

interface PastThreadsProps {
agentId: string;
currentThreadId?: string | null;
onThreadSelect: (threadId: string) => void;
}

export const PastThreads: React.FC<PastThreadsProps> = ({
agentId,
currentThreadId,
onThreadSelect,
}) => {
const [open, setOpen] = useState(false);
Expand All @@ -58,6 +60,19 @@ export const PastThreads: React.FC<PastThreadsProps> = ({
setOpen(false);
};

const sortedThreads =
currentThreadId && threads
? [...threads].sort((a, b) => {
if (a.id === currentThreadId) {
return -1;
}
if (b.id === currentThreadId) {
return 1;
}
return 0;
})
: (threads ?? []);

return (
<Tooltip>
<TooltipContent>Switch threads</TooltipContent>
Expand Down Expand Up @@ -86,26 +101,35 @@ export const PastThreads: React.FC<PastThreadsProps> = ({
</div>
) : threads && threads.length > 0 ? (
<CommandGroup>
{threads.map((thread: Thread) => (
{sortedThreads.map((thread: Thread) => (
<CommandItem
key={thread.id}
onSelect={() =>
handleThreadSelect(thread.id)
}
className="cursor-pointer"
>
<div>
<TypographyP className="font-semibold">
Thread
<span className="ml-2 text-muted-foreground">
{thread.id}
</span>
</TypographyP>
<TypographyP className="text-sm text-gray-500">
{new Date(
thread.created
).toLocaleString()}
</TypographyP>
<div className="flex items-center justify-between w-full">
<div>
<TypographyP className="font-semibold">
Thread
<span className="ml-2 text-muted-foreground">
{thread.id}
</span>
</TypographyP>
<TypographyP className="text-sm text-gray-500">
{new Date(
thread.created
).toLocaleString()}
</TypographyP>
</div>
<div>
{currentThreadId &&
thread.id ===
currentThreadId && (
<CheckIcon />
)}
</div>
</div>
</CommandItem>
))}
Expand Down
6 changes: 5 additions & 1 deletion ui/admin/app/routes/_auth.agents.$agent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export default function ChatAgent() {
<ResizablePanelGroup direction="horizontal" className="flex-auto">
<ResizablePanel className="">
<AgentProvider agent={agent}>
<Agent onRefresh={updateThreadId} key={agent.id} />
<Agent
currentThreadId={threadId}
onRefresh={updateThreadId}
key={agent.id}
/>
</AgentProvider>
</ResizablePanel>
<ResizableHandle withHandle />
Expand Down

0 comments on commit 6d38bb7

Please sign in to comment.