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
This commit is contained in:
suyao 2025-09-20 16:41:15 +08:00
parent 3ae1b3d4cb
commit d960a42d6e
No known key found for this signature in database
2 changed files with 20 additions and 3 deletions

View File

@ -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<AssistantsTabProps> = () => {
[dispatch]
)
useEffect(() => {
if (!isLoading && agents.length > 0 && !activeAgentId) {
setActiveAgentId(agents[0].id)
}
}, [isLoading, agents, activeAgentId, setActiveAgentId])
return (
<div className="agents-tab h-full w-full p-2">
{isLoading && <Spinner />}

View File

@ -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<SessionsProps> = ({ 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<SessionsProps> = ({ 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 (
<motion.div