File size: 6,174 Bytes
9b906ea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | import { useTranslation } from "react-i18next";
import { useEffect, useState } from "react";
import { CircleCheck } from "lucide-react";
import { useActiveConversation } from "#/hooks/query/use-active-conversation";
import { getStatusCode } from "#/utils/status";
import { ChatStopButton } from "../chat/chat-stop-button";
import { AgentState } from "#/types/agent-state";
import ClockIcon from "#/icons/u-clock-three.svg?react";
import { ChatResumeAgentButton } from "../chat/chat-play-button";
import { cn, isTaskPolling } from "#/utils/utils";
import { AgentLoading } from "./agent-loading";
import { useConversationStore } from "#/stores/conversation-store";
import CircleErrorIcon from "#/icons/circle-error.svg?react";
import { useAgentState } from "#/hooks/use-agent-state";
import { useUnifiedWebSocketStatus } from "#/hooks/use-unified-websocket-status";
import { useTaskPolling } from "#/hooks/query/use-task-polling";
import { useSubConversationTaskPolling } from "#/hooks/query/use-sub-conversation-task-polling";
import { useAgentNotification } from "#/hooks/use-agent-notification";
import { I18nKey } from "#/i18n/declaration";
export interface AgentStatusProps {
className?: string;
handleStop: () => void;
handleResumeAgent: () => void;
disabled?: boolean;
isPausing?: boolean;
}
export function AgentStatus({
className = "",
handleStop,
handleResumeAgent,
disabled = false,
isPausing = false,
}: AgentStatusProps) {
const { t } = useTranslation("openhands");
const { setShouldShownAgentLoading } = useConversationStore();
const { curAgentState, executionStatus } = useAgentState();
// Trigger browser tab flash and notification sound on state changes
useAgentNotification(curAgentState);
const webSocketStatus = useUnifiedWebSocketStatus();
const { data: conversation } = useActiveConversation();
const { taskStatus } = useTaskPolling();
const { subConversationTaskId } = useConversationStore();
// Poll sub-conversation task to track its loading state
const { taskStatus: subConversationTaskStatus } =
useSubConversationTaskPolling(
subConversationTaskId,
conversation?.id || null,
);
const statusCode = getStatusCode(
webSocketStatus,
executionStatus ?? null,
taskStatus,
subConversationTaskStatus,
);
const shouldShownAgentLoading =
curAgentState === AgentState.INIT ||
curAgentState === AgentState.LOADING ||
(webSocketStatus === "CONNECTING" && taskStatus !== "ERROR") ||
isTaskPolling(taskStatus) ||
isTaskPolling(subConversationTaskStatus);
// For UI rendering - includes pause state
const isLoading = shouldShownAgentLoading || isPausing;
const shouldShownAgentError =
curAgentState === AgentState.ERROR ||
curAgentState === AgentState.RATE_LIMITED ||
webSocketStatus === "CLOSED" ||
taskStatus === "ERROR";
const shouldShownAgentStop =
!shouldShownAgentError && curAgentState === AgentState.RUNNING;
const shouldShownAgentResume =
!shouldShownAgentError &&
(curAgentState === AgentState.STOPPED ||
curAgentState === AgentState.PAUSED);
const isInteractive =
!isLoading && (shouldShownAgentStop || shouldShownAgentResume);
const isDoneStatus =
statusCode === I18nKey.CHAT_INTERFACE$AGENT_FINISHED_MESSAGE;
const isReadyStatus = statusCode === I18nKey.AGENT_STATUS$WAITING_FOR_TASK;
const isTransientCheckStatus = isDoneStatus || isReadyStatus;
const [shouldRenderDoneStatus, setShouldRenderDoneStatus] = useState(true);
const [shouldFadeDoneStatus, setShouldFadeDoneStatus] = useState(false);
// Update global state when agent loading condition changes
useEffect(() => {
setShouldShownAgentLoading(!!shouldShownAgentLoading);
}, [shouldShownAgentLoading, setShouldShownAgentLoading]);
useEffect(() => {
if (!isTransientCheckStatus) {
setShouldRenderDoneStatus(true);
setShouldFadeDoneStatus(false);
return;
}
setShouldRenderDoneStatus(true);
setShouldFadeDoneStatus(false);
const fadeTimer = window.setTimeout(() => {
setShouldFadeDoneStatus(true);
}, 1000);
const hideTimer = window.setTimeout(() => {
setShouldRenderDoneStatus(false);
}, 1500);
return () => {
window.clearTimeout(fadeTimer);
window.clearTimeout(hideTimer);
};
}, [isTransientCheckStatus]);
if (isTransientCheckStatus && !shouldRenderDoneStatus) {
return null;
}
return (
<div
className={cn(
"flex items-center gap-1 min-w-0",
isTransientCheckStatus && "transition-opacity duration-500",
shouldFadeDoneStatus && "opacity-0",
className,
)}
>
<span
className="text-[11px] text-[var(--oh-muted)] font-normal leading-5 min-w-0 max-w-full truncate"
title={t(statusCode)}
>
{t(statusCode)}
</span>
<div
className={cn(
"box-border content-stretch flex flex-row gap-[3px] items-center justify-center overflow-clip px-0.5 py-1 relative rounded-[100px] shrink-0 size-6 transition-all duration-200 active:scale-95 bg-transparent text-[var(--oh-muted)] hover:bg-white/10 hover:text-white",
isInteractive ? "cursor-pointer" : "cursor-default",
)}
>
{isLoading && <AgentLoading />}
{!isLoading && shouldShownAgentStop && (
<ChatStopButton handleStop={handleStop} />
)}
{!isLoading && shouldShownAgentResume && (
<ChatResumeAgentButton
onAgentResumed={handleResumeAgent}
disabled={disabled}
/>
)}
{!isLoading && shouldShownAgentError && (
<CircleErrorIcon
className="w-4 h-4 text-current"
data-testid="circle-error-icon"
/>
)}
{!isLoading &&
!shouldShownAgentStop &&
!shouldShownAgentResume &&
!shouldShownAgentError &&
(isTransientCheckStatus ? (
<CircleCheck className="w-4 h-4 text-current" />
) : (
<ClockIcon className="w-4 h-4 text-current" />
))}
</div>
</div>
);
}
export default AgentStatus;
|