Skip to content

Commit

Permalink
Display connection rejection errors passed to client
Browse files Browse the repository at this point in the history
  • Loading branch information
raymyers committed Jan 7, 2025
1 parent 0973446 commit c26e9fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
24 changes: 20 additions & 4 deletions frontend/src/context/ws-client-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import posthog from "posthog-js";
import React from "react";
import { io, Socket } from "socket.io-client";
import EventLogger from "#/utils/event-logger";
import { handleAssistantMessage } from "#/services/actions";
import {
handleAssistantMessage,
handleStatusMessage,
} from "#/services/actions";
import { useRate } from "#/hooks/use-rate";
import { OpenHandsParsedEvent } from "#/types/core";
import {
Expand Down Expand Up @@ -104,19 +107,32 @@ export function WsClientProvider({
handleAssistantMessage(event);
}

Check failure on line 108 in frontend/src/context/ws-client-provider.tsx

View workflow job for this annotation

GitHub Actions / Lint frontend

Replace `data·&&·typeof·data·===·'object'·&&·'message'·in·data` with `⏎······data·&&⏎······typeof·data·===·"object"·&&⏎······"message"·in·data·&&`

Check failure on line 109 in frontend/src/context/ws-client-provider.tsx

View workflow job for this annotation

GitHub Actions / Lint frontend

Replace `·&&·typeof·data.message·===·"string"` with `·typeof·data.message·===·"string"⏎····`
function handleDisconnect() {
function updateStatusWhenErrorMessagePresent(data: unknown) {
if (data && typeof data === 'object' && 'message' in data
&& typeof data.message === "string") {
handleStatusMessage({
type: "error",
message: data.message as string,
status_update: true,
});
}
}

function handleDisconnect(data: unknown) {
setStatus(WsClientProviderStatus.DISCONNECTED);
const sio = sioRef.current;
if (!sio) {
return;
}
sio.io.opts.query = sio.io.opts.query || {};
sio.io.opts.query.latest_event_id = lastEventRef.current?.id;
updateStatusWhenErrorMessagePresent(data);
}

function handleError() {
posthog.capture("socket_error");
function handleError(data: unknown) {
setStatus(WsClientProviderStatus.DISCONNECTED);
updateStatusWhenErrorMessagePresent(data);
posthog.capture("socket_error");
}

React.useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function handleActionMessage(message: ActionMessage) {
if (message.args && message.args.thought) {
store.dispatch(addAssistantMessage(message.args.thought));
}
// Need to convert ActionMessage to RejectAction
// @ts-expect-error TODO: fix
store.dispatch(addAssistantAction(message));
}
Expand Down
13 changes: 8 additions & 5 deletions frontend/src/state/chat-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const chatSlice = createSlice({
state.messages.push(message);
},

addAssistantMessage(state, action: PayloadAction<string>) {
addAssistantMessage(state: SliceState, action: PayloadAction<string>) {
const message: Message = {
type: "thought",
sender: "assistant",
Expand All @@ -85,7 +85,10 @@ export const chatSlice = createSlice({
state.messages.push(message);
},

addAssistantAction(state, action: PayloadAction<OpenHandsAction>) {
addAssistantAction(
state: SliceState,
action: PayloadAction<OpenHandsAction>,
) {
const actionID = action.payload.action;
if (!HANDLED_ACTIONS.includes(actionID)) {
return;
Expand Down Expand Up @@ -125,7 +128,7 @@ export const chatSlice = createSlice({
},

addAssistantObservation(
state,
state: SliceState,
observation: PayloadAction<OpenHandsObservation>,
) {
const observationID = observation.payload.observation;
Expand Down Expand Up @@ -179,7 +182,7 @@ export const chatSlice = createSlice({
},

addErrorMessage(
state,
state: SliceState,
action: PayloadAction<{ id?: string; message: string }>,
) {
const { id, message } = action.payload;
Expand All @@ -192,7 +195,7 @@ export const chatSlice = createSlice({
});
},

clearMessages(state) {
clearMessages(state: SliceState) {
state.messages = [];
},
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ export interface ObservationMessage {
export interface StatusMessage {
status_update: true;
type: string;
id: string;
id?: string;
message: string;
}

0 comments on commit c26e9fd

Please sign in to comment.