Browse Source

fix messages (#5421)

Robert Brennan 1 year ago
parent
commit
ea96ffca9b

+ 0 - 1
frontend/src/components/features/chat/messages.tsx

@@ -14,7 +14,6 @@ export function Messages({
 }: MessagesProps) {
   return messages.map((message, index) => {
     if (message.type === "error" || message.type === "action") {
-      console.log("expando", message);
       return (
         <ExpandableMessage
           key={index}

+ 9 - 119
frontend/src/services/actions.ts

@@ -4,12 +4,9 @@ import {
   addUserMessage,
   addErrorMessage,
 } from "#/state/chat-slice";
+import { appendSecurityAnalyzerInput } from "#/state/security-analyzer-slice";
 import { setCode, setActiveFilepath } from "#/state/code-slice";
 import { appendJupyterInput } from "#/state/jupyter-slice";
-import {
-  ActionSecurityRisk,
-  appendSecurityAnalyzerInput,
-} from "#/state/security-analyzer-slice";
 import { setCurStatusMessage } from "#/state/status-slice";
 import store from "#/store";
 import ActionType from "#/types/action-type";
@@ -51,6 +48,8 @@ const messageActions = {
           pending: false,
         }),
       );
+    } else {
+      store.dispatch(addAssistantMessage(message.args.content));
     }
   },
   [ActionType.RUN_IPYTHON]: (message: ActionMessage) => {
@@ -60,130 +59,21 @@ const messageActions = {
   },
 };
 
-function getRiskText(risk: ActionSecurityRisk) {
-  switch (risk) {
-    case ActionSecurityRisk.LOW:
-      return "Low Risk";
-    case ActionSecurityRisk.MEDIUM:
-      return "Medium Risk";
-    case ActionSecurityRisk.HIGH:
-      return "High Risk";
-    case ActionSecurityRisk.UNKNOWN:
-    default:
-      return "Unknown Risk";
+export function handleActionMessage(message: ActionMessage) {
+  if (message.args?.hidden) {
+    return;
   }
-}
 
-export function handleActionMessage(message: ActionMessage) {
   if ("args" in message && "security_risk" in message.args) {
     store.dispatch(appendSecurityAnalyzerInput(message));
   }
 
-  if (
-    (message.action === ActionType.RUN ||
-      message.action === ActionType.RUN_IPYTHON) &&
-    message.args.confirmation_state === "awaiting_confirmation"
-  ) {
-    if (message.args.thought) {
-      store.dispatch(addAssistantMessage(message.args.thought));
-    }
-    if (message.args.command) {
-      store.dispatch(
-        addAssistantMessage(
-          `Running this command now: \n\`\`\`\`bash\n${message.args.command}\n\`\`\`\`\nEstimated security risk: ${getRiskText(message.args.security_risk as unknown as ActionSecurityRisk)}`,
-        ),
-      );
-    } else if (message.args.code) {
-      store.dispatch(
-        addAssistantMessage(
-          `Running this code now: \n\`\`\`\`python\n${message.args.code}\n\`\`\`\`\nEstimated security risk: ${getRiskText(message.args.security_risk as unknown as ActionSecurityRisk)}`,
-        ),
-      );
-    } else {
-      store.dispatch(addAssistantMessage(message.message));
-    }
-    return;
-  }
-
-  if (message.source !== "user" && !message.args?.hidden) {
+  if (message.source === "agent") {
     if (message.args && message.args.thought) {
       store.dispatch(addAssistantMessage(message.args.thought));
     }
-    // Convert the message to a properly typed action
-    const baseAction = {
-      ...message,
-      source: "agent" as const,
-      args: {
-        ...message.args,
-        thought: message.args?.thought || message.message || "",
-      },
-    };
-
-    // Cast to the appropriate action type based on the action field
-    switch (message.action) {
-      case "run":
-        store.dispatch(
-          addAssistantAction({
-            ...baseAction,
-            action: "run" as const,
-            args: {
-              command: String(message.args?.command || ""),
-              confirmation_state: (message.args?.confirmation_state ||
-                "confirmed") as
-                | "confirmed"
-                | "rejected"
-                | "awaiting_confirmation",
-              thought: String(message.args?.thought || message.message || ""),
-              hidden: Boolean(message.args?.hidden),
-            },
-          }),
-        );
-        break;
-      case "message":
-        store.dispatch(
-          addAssistantAction({
-            ...baseAction,
-            action: "message" as const,
-            args: {
-              content: String(message.args?.content || message.message || ""),
-              image_urls: Array.isArray(message.args?.image_urls)
-                ? message.args.image_urls
-                : null,
-              wait_for_response: Boolean(message.args?.wait_for_response),
-            },
-          }),
-        );
-        break;
-      case "run_ipython":
-        store.dispatch(
-          addAssistantAction({
-            ...baseAction,
-            action: "run_ipython" as const,
-            args: {
-              code: String(message.args?.code || ""),
-              confirmation_state: (message.args?.confirmation_state ||
-                "confirmed") as
-                | "confirmed"
-                | "rejected"
-                | "awaiting_confirmation",
-              kernel_init_code: String(message.args?.kernel_init_code || ""),
-              thought: String(message.args?.thought || message.message || ""),
-            },
-          }),
-        );
-        break;
-      default:
-        // For other action types, ensure we have the required thought property
-        store.dispatch(
-          addAssistantAction({
-            ...baseAction,
-            action: "reject" as const,
-            args: {
-              thought: String(message.args?.thought || message.message || ""),
-            },
-          }),
-        );
-    }
+    // @ts-expect-error TODO: fix
+    store.dispatch(addAssistantAction(message));
   }
 
   if (message.action in messageActions) {

+ 22 - 1
frontend/src/state/chat-slice.ts

@@ -1,5 +1,6 @@
 import { createSlice, PayloadAction } from "@reduxjs/toolkit";
 
+import { ActionSecurityRisk } from "#/state/security-analyzer-slice";
 import { OpenHandsObservation } from "#/types/core/observations";
 import { OpenHandsAction } from "#/types/core/actions";
 
@@ -9,6 +10,20 @@ const MAX_CONTENT_LENGTH = 1000;
 
 const HANDLED_ACTIONS = ["run", "run_ipython", "write", "read"];
 
+function getRiskText(risk: ActionSecurityRisk) {
+  switch (risk) {
+    case ActionSecurityRisk.LOW:
+      return "Low Risk";
+    case ActionSecurityRisk.MEDIUM:
+      return "Medium Risk";
+    case ActionSecurityRisk.HIGH:
+      return "High Risk";
+    case ActionSecurityRisk.UNKNOWN:
+    default:
+      return "Unknown Risk";
+  }
+}
+
 const initialState: SliceState = {
   messages: [],
 };
@@ -78,6 +93,13 @@ export const chatSlice = createSlice({
       } else if (actionID === "read") {
         text = action.payload.args.path;
       }
+      if (actionID === "run" || actionID === "run_ipython") {
+        if (
+          action.payload.args.confirmation_state === "awaiting_confirmation"
+        ) {
+          text += `\n\n${getRiskText(action.payload.args.security_risk as unknown as ActionSecurityRisk)}`;
+        }
+      }
       const message: Message = {
         type: "action",
         sender: "assistant",
@@ -122,7 +144,6 @@ export const chatSlice = createSlice({
       action: PayloadAction<{ id?: string; message: string }>,
     ) {
       const { id, message } = action.payload;
-      console.log("add err message", id, message);
       state.messages.push({
         translationID: id,
         content: message,

+ 3 - 0
frontend/src/types/core/actions.ts

@@ -1,4 +1,5 @@
 import { OpenHandsActionEvent } from "./base";
+import { ActionSecurityRisk } from "#/state/security-analyzer-slice";
 
 export interface UserMessageAction extends OpenHandsActionEvent<"message"> {
   source: "user";
@@ -12,6 +13,7 @@ export interface CommandAction extends OpenHandsActionEvent<"run"> {
   source: "agent";
   args: {
     command: string;
+    security_risk: ActionSecurityRisk;
     confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
     thought: string;
     hidden?: boolean;
@@ -32,6 +34,7 @@ export interface IPythonAction extends OpenHandsActionEvent<"run_ipython"> {
   source: "agent";
   args: {
     code: string;
+    security_risk: ActionSecurityRisk;
     confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
     kernel_init_code: string;
     thought: string;