From d960a42d6e72dc9849ebda6ef4730cb45c236cd3 Mon Sep 17 00:00:00 2001 From: suyao Date: Sat, 20 Sep 2025 16:41:15 +0800 Subject: [PATCH] Set default active agent and session on load - Automatically select first agent if none active after loading - Automatically select first session per agent if none active after loading - Prevent empty selection states in UI components --- src/renderer/src/pages/home/Tabs/AgentsTab.tsx | 8 +++++++- .../src/pages/home/Tabs/components/Sessions.tsx | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/pages/home/Tabs/AgentsTab.tsx b/src/renderer/src/pages/home/Tabs/AgentsTab.tsx index 96ba8e449e..a4f30d9d26 100644 --- a/src/renderer/src/pages/home/Tabs/AgentsTab.tsx +++ b/src/renderer/src/pages/home/Tabs/AgentsTab.tsx @@ -5,7 +5,7 @@ import { useRuntime } from '@renderer/hooks/useRuntime' import { useAppDispatch } from '@renderer/store' import { setActiveAgentId as setActiveAgentIdAction } from '@renderer/store/runtime' import { Plus } from 'lucide-react' -import { FC, useCallback } from 'react' +import { FC, useCallback, useEffect } from 'react' import { useTranslation } from 'react-i18next' import AgentItem from './components/AgentItem' @@ -27,6 +27,12 @@ export const AgentsTab: FC = () => { [dispatch] ) + useEffect(() => { + if (!isLoading && agents.length > 0 && !activeAgentId) { + setActiveAgentId(agents[0].id) + } + }, [isLoading, agents, activeAgentId, setActiveAgentId]) + return (
{isLoading && } diff --git a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx index 5de522d49f..73ceff87f0 100644 --- a/src/renderer/src/pages/home/Tabs/components/Sessions.tsx +++ b/src/renderer/src/pages/home/Tabs/components/Sessions.tsx @@ -1,11 +1,12 @@ import { Button, Spinner } from '@heroui/react' import { SessionModal } from '@renderer/components/Popups/agent/SessionModal' import { useSessions } from '@renderer/hooks/agents/useSessions' +import { useRuntime } from '@renderer/hooks/useRuntime' import { useAppDispatch } from '@renderer/store' import { setActiveSessionIdAction, setActiveTopicOrSessionAction } from '@renderer/store/runtime' -import { AnimatePresence,motion } from 'framer-motion' +import { AnimatePresence, motion } from 'framer-motion' import { Plus } from 'lucide-react' -import { memo, useCallback } from 'react' +import { memo, useCallback, useEffect } from 'react' import { useTranslation } from 'react-i18next' import SessionItem from './SessionItem' @@ -19,6 +20,8 @@ interface SessionsProps { const Sessions: React.FC = ({ agentId }) => { const { t } = useTranslation() const { sessions, isLoading, deleteSession } = useSessions(agentId) + const { chat } = useRuntime() + const { activeSessionId } = chat const dispatch = useAppDispatch() const setActiveSessionId = useCallback( @@ -29,6 +32,14 @@ const Sessions: React.FC = ({ agentId }) => { [dispatch] ) + const currentActiveSessionId = activeSessionId[agentId] + + useEffect(() => { + if (!isLoading && sessions.length > 0 && !currentActiveSessionId) { + setActiveSessionId(agentId, sessions[0].id) + } + }, [isLoading, sessions, currentActiveSessionId, agentId, setActiveSessionId]) + if (isLoading) { return (