mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 13:52:43 +08:00
fix messages (#5421)
This commit is contained in:
parent
7ec407dc50
commit
ea96ffca9b
@ -14,7 +14,6 @@ export function Messages({
|
|||||||
}: MessagesProps) {
|
}: MessagesProps) {
|
||||||
return messages.map((message, index) => {
|
return messages.map((message, index) => {
|
||||||
if (message.type === "error" || message.type === "action") {
|
if (message.type === "error" || message.type === "action") {
|
||||||
console.log("expando", message);
|
|
||||||
return (
|
return (
|
||||||
<ExpandableMessage
|
<ExpandableMessage
|
||||||
key={index}
|
key={index}
|
||||||
|
|||||||
@ -4,12 +4,9 @@ import {
|
|||||||
addUserMessage,
|
addUserMessage,
|
||||||
addErrorMessage,
|
addErrorMessage,
|
||||||
} from "#/state/chat-slice";
|
} from "#/state/chat-slice";
|
||||||
|
import { appendSecurityAnalyzerInput } from "#/state/security-analyzer-slice";
|
||||||
import { setCode, setActiveFilepath } from "#/state/code-slice";
|
import { setCode, setActiveFilepath } from "#/state/code-slice";
|
||||||
import { appendJupyterInput } from "#/state/jupyter-slice";
|
import { appendJupyterInput } from "#/state/jupyter-slice";
|
||||||
import {
|
|
||||||
ActionSecurityRisk,
|
|
||||||
appendSecurityAnalyzerInput,
|
|
||||||
} from "#/state/security-analyzer-slice";
|
|
||||||
import { setCurStatusMessage } from "#/state/status-slice";
|
import { setCurStatusMessage } from "#/state/status-slice";
|
||||||
import store from "#/store";
|
import store from "#/store";
|
||||||
import ActionType from "#/types/action-type";
|
import ActionType from "#/types/action-type";
|
||||||
@ -51,6 +48,8 @@ const messageActions = {
|
|||||||
pending: false,
|
pending: false,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
store.dispatch(addAssistantMessage(message.args.content));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[ActionType.RUN_IPYTHON]: (message: ActionMessage) => {
|
[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) {
|
export function handleActionMessage(message: ActionMessage) {
|
||||||
|
if (message.args?.hidden) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ("args" in message && "security_risk" in message.args) {
|
if ("args" in message && "security_risk" in message.args) {
|
||||||
store.dispatch(appendSecurityAnalyzerInput(message));
|
store.dispatch(appendSecurityAnalyzerInput(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (message.source === "agent") {
|
||||||
(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.args && message.args.thought) {
|
if (message.args && message.args.thought) {
|
||||||
store.dispatch(addAssistantMessage(message.args.thought));
|
store.dispatch(addAssistantMessage(message.args.thought));
|
||||||
}
|
}
|
||||||
// Convert the message to a properly typed action
|
// @ts-expect-error TODO: fix
|
||||||
const baseAction = {
|
store.dispatch(addAssistantAction(message));
|
||||||
...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 || ""),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.action in messageActions) {
|
if (message.action in messageActions) {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
|
import { ActionSecurityRisk } from "#/state/security-analyzer-slice";
|
||||||
import { OpenHandsObservation } from "#/types/core/observations";
|
import { OpenHandsObservation } from "#/types/core/observations";
|
||||||
import { OpenHandsAction } from "#/types/core/actions";
|
import { OpenHandsAction } from "#/types/core/actions";
|
||||||
|
|
||||||
@ -9,6 +10,20 @@ const MAX_CONTENT_LENGTH = 1000;
|
|||||||
|
|
||||||
const HANDLED_ACTIONS = ["run", "run_ipython", "write", "read"];
|
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 = {
|
const initialState: SliceState = {
|
||||||
messages: [],
|
messages: [],
|
||||||
};
|
};
|
||||||
@ -78,6 +93,13 @@ export const chatSlice = createSlice({
|
|||||||
} else if (actionID === "read") {
|
} else if (actionID === "read") {
|
||||||
text = action.payload.args.path;
|
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 = {
|
const message: Message = {
|
||||||
type: "action",
|
type: "action",
|
||||||
sender: "assistant",
|
sender: "assistant",
|
||||||
@ -122,7 +144,6 @@ export const chatSlice = createSlice({
|
|||||||
action: PayloadAction<{ id?: string; message: string }>,
|
action: PayloadAction<{ id?: string; message: string }>,
|
||||||
) {
|
) {
|
||||||
const { id, message } = action.payload;
|
const { id, message } = action.payload;
|
||||||
console.log("add err message", id, message);
|
|
||||||
state.messages.push({
|
state.messages.push({
|
||||||
translationID: id,
|
translationID: id,
|
||||||
content: message,
|
content: message,
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { OpenHandsActionEvent } from "./base";
|
import { OpenHandsActionEvent } from "./base";
|
||||||
|
import { ActionSecurityRisk } from "#/state/security-analyzer-slice";
|
||||||
|
|
||||||
export interface UserMessageAction extends OpenHandsActionEvent<"message"> {
|
export interface UserMessageAction extends OpenHandsActionEvent<"message"> {
|
||||||
source: "user";
|
source: "user";
|
||||||
@ -12,6 +13,7 @@ export interface CommandAction extends OpenHandsActionEvent<"run"> {
|
|||||||
source: "agent";
|
source: "agent";
|
||||||
args: {
|
args: {
|
||||||
command: string;
|
command: string;
|
||||||
|
security_risk: ActionSecurityRisk;
|
||||||
confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
|
confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
|
||||||
thought: string;
|
thought: string;
|
||||||
hidden?: boolean;
|
hidden?: boolean;
|
||||||
@ -32,6 +34,7 @@ export interface IPythonAction extends OpenHandsActionEvent<"run_ipython"> {
|
|||||||
source: "agent";
|
source: "agent";
|
||||||
args: {
|
args: {
|
||||||
code: string;
|
code: string;
|
||||||
|
security_risk: ActionSecurityRisk;
|
||||||
confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
|
confirmation_state: "confirmed" | "rejected" | "awaiting_confirmation";
|
||||||
kernel_init_code: string;
|
kernel_init_code: string;
|
||||||
thought: string;
|
thought: string;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user