import { useRef, useState } from 'react'; import { seedManifest } from '../../app/constants'; import type { AgentBackgroundSubmitMode, AgentChatInteractionMode, AgentChatPendingRuntimeRun, AgentChatReplyPhase, AgentConversationSessionRecord, AgentGoalDialogState, AgentGoalRecord, AgentRuntimeState, GameCreatorLlmConfigStatus, LocalConversationMessageRecord, TauriInvoke, } from '../../app/types'; import { deriveAgentStatusCards } from '../project-summary/agentPresentation'; export function useDeveloperAgentState() { const launcherAgentChatAgents = deriveAgentStatusCards(seedManifest, null); const [agentChatProjectPath, setAgentChatProjectPath] = useState(''); const [agentChatSelectedAgentId, setAgentChatSelectedAgentId] = useState( launcherAgentChatAgents[0]?.id ?? '', ); const [agentChatMessages, setAgentChatMessages] = useState< LocalConversationMessageRecord[] >([]); const [agentChatSessions, setAgentChatSessions] = useState< AgentConversationSessionRecord[] >([]); const [agentChatSelectedSessionId, setAgentChatSelectedSessionId] = useState< string | null >(null); const [agentChatActiveSessionId, setAgentChatActiveSessionId] = useState< string | null >(null); const [agentChatLegacySessionMode, setAgentChatLegacySessionMode] = useState(false); const [agentChatConversationPath, setAgentChatConversationPath] = useState(''); const [agentChatSessionStatus, setAgentChatSessionStatus] = useState(''); const [agentChatInput, setAgentChatInput] = useState(''); const [agentChatInteractionMode, setAgentChatInteractionMode] = useState('run'); const [agentChatRunSubmitMode, setAgentChatRunSubmitMode] = useState('steer'); const [agentChatReplyPhase, setAgentChatReplyPhase] = useState('idle'); const [agentChatStatus, setAgentChatStatus] = useState('请选择项目和 Agent'); const [agentChatBusy, setAgentChatBusy] = useState(false); const agentChatMessagesRef = useRef(null); const agentChatShouldFollowLatestRef = useRef(true); const [agentChatBackgroundBusy, setAgentChatBackgroundBusy] = useState(false); const [agentChatPendingRuntimeRun, setAgentChatPendingRuntimeRunState] = useState(null); const [agentChatLlmConfigStatus, setAgentChatLlmConfigStatus] = useState(null); const [agentChatLlmStatus, setAgentChatLlmStatus] = useState('尚未检查 LLM 配置'); const [agentChatRuntime, setAgentChatRuntime] = useState(null); const [agentChatActiveRuntime, setAgentChatActiveRuntime] = useState(null); const [agentChatRuntimeError, setAgentChatRuntimeError] = useState(''); const [agentChatGoal, setAgentChatGoal] = useState( null, ); const [agentChatGoalError, setAgentChatGoalError] = useState(''); const [agentChatGoalDialog, setAgentChatGoalDialog] = useState(null); const [agentChatGoalDialogError, setAgentChatGoalDialogError] = useState(''); const [agentChatResumeConfirmation, setAgentChatResumeConfirmation] = useState<{ projectPath: string; detail: string } | null>(null); const agentChatLoadVersionRef = useRef(0); const agentChatRuntimeResumeProjectPathRef = useRef(null); const agentChatProjectPathRef = useRef(agentChatProjectPath); agentChatProjectPathRef.current = agentChatProjectPath; const agentChatSelectedAgentIdRef = useRef(agentChatSelectedAgentId); agentChatSelectedAgentIdRef.current = agentChatSelectedAgentId; const agentChatSelectedSessionIdRef = useRef(agentChatSelectedSessionId); agentChatSelectedSessionIdRef.current = agentChatSelectedSessionId; const agentChatActiveSessionIdRef = useRef(agentChatActiveSessionId); agentChatActiveSessionIdRef.current = agentChatActiveSessionId; const agentChatPendingRuntimeRunRef = useRef(null); const agentChatRuntimeSyncingRunIdsRef = useRef(new Set()); const agentChatRuntimeSyncConversationRef = useRef< | (( invoke: TauriInvoke, pendingRun: AgentChatPendingRuntimeRun, ) => Promise) | null >(null); return { launcherAgentChatAgents, agentChatProjectPath, setAgentChatProjectPath, agentChatSelectedAgentId, setAgentChatSelectedAgentId, agentChatMessages, setAgentChatMessages, agentChatSessions, setAgentChatSessions, agentChatSelectedSessionId, setAgentChatSelectedSessionId, agentChatActiveSessionId, setAgentChatActiveSessionId, agentChatLegacySessionMode, setAgentChatLegacySessionMode, agentChatConversationPath, setAgentChatConversationPath, agentChatSessionStatus, setAgentChatSessionStatus, agentChatInput, setAgentChatInput, agentChatInteractionMode, setAgentChatInteractionMode, agentChatRunSubmitMode, setAgentChatRunSubmitMode, agentChatReplyPhase, setAgentChatReplyPhase, agentChatStatus, setAgentChatStatus, agentChatBusy, setAgentChatBusy, agentChatMessagesRef, agentChatShouldFollowLatestRef, agentChatBackgroundBusy, setAgentChatBackgroundBusy, agentChatPendingRuntimeRun, setAgentChatPendingRuntimeRunState, agentChatLlmConfigStatus, setAgentChatLlmConfigStatus, agentChatLlmStatus, setAgentChatLlmStatus, agentChatRuntime, setAgentChatRuntime, agentChatActiveRuntime, setAgentChatActiveRuntime, agentChatRuntimeError, setAgentChatRuntimeError, agentChatGoal, setAgentChatGoal, agentChatGoalError, setAgentChatGoalError, agentChatGoalDialog, setAgentChatGoalDialog, agentChatGoalDialogError, setAgentChatGoalDialogError, agentChatResumeConfirmation, setAgentChatResumeConfirmation, agentChatLoadVersionRef, agentChatRuntimeResumeProjectPathRef, agentChatProjectPathRef, agentChatSelectedAgentIdRef, agentChatSelectedSessionIdRef, agentChatActiveSessionIdRef, agentChatPendingRuntimeRunRef, agentChatRuntimeSyncingRunIdsRef, agentChatRuntimeSyncConversationRef, }; } export type DeveloperAgentState = ReturnType;