Skip to content

Commit

Permalink
fix(frontend): Only render loading indicator if events are messages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape authored Jan 6, 2025
1 parent 17d722f commit 0973446
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions frontend/src/context/ws-client-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,39 @@ import EventLogger from "#/utils/event-logger";
import { handleAssistantMessage } from "#/services/actions";
import { useRate } from "#/hooks/use-rate";
import { OpenHandsParsedEvent } from "#/types/core";
import { AgentStateChangeObservation } from "#/types/core/observations";
import {
AssistantMessageAction,
UserMessageAction,
} from "#/types/core/actions";

const isOpenHandsMessage = (event: unknown): event is OpenHandsParsedEvent =>
const isOpenHandsEvent = (event: unknown): event is OpenHandsParsedEvent =>
typeof event === "object" &&
event !== null &&
"id" in event &&
"source" in event &&
"message" in event &&
"timestamp" in event;

const isAgentStateChangeObservation = (
const isUserMessage = (
event: OpenHandsParsedEvent,
): event is AgentStateChangeObservation =>
"observation" in event && event.observation === "agent_state_changed";
): event is UserMessageAction =>
"source" in event &&
"type" in event &&
event.source === "user" &&
event.type === "message";

const isAssistantMessage = (
event: OpenHandsParsedEvent,
): event is AssistantMessageAction =>
"source" in event &&
"type" in event &&
event.source === "agent" &&
event.type === "message";

const isMessageAction = (
event: OpenHandsParsedEvent,
): event is UserMessageAction | AssistantMessageAction =>
isUserMessage(event) || isAssistantMessage(event);

export enum WsClientProviderStatus {
CONNECTED,
Expand Down Expand Up @@ -74,7 +93,7 @@ export function WsClientProvider({
}

function handleMessage(event: Record<string, unknown>) {
if (isOpenHandsMessage(event) && !isAgentStateChangeObservation(event)) {
if (isOpenHandsEvent(event) && isMessageAction(event)) {
messageRateHandler.record(new Date().getTime());
}
setEvents((prevEvents) => [...prevEvents, event]);
Expand Down

0 comments on commit 0973446

Please sign in to comment.