π OpenHands: AI-Driven Development
refactor(frontend): migration of security-analyzer-slice.ts to zustand (#11082)
Hiep Lea94906e15c65
diff --git a/frontend/src/components/features/chat/event-content-helpers/get-action-content.ts b/frontend/src/components/features/chat/event-content-helpers/get-action-content.tsindex 0c27ef6eda..a04be2c385 100644--- a/frontend/src/components/features/chat/event-content-helpers/get-action-content.ts+++ b/frontend/src/components/features/chat/event-content-helpers/get-action-content.ts@@ -1,4 +1,4 @@-import { ActionSecurityRisk } from "#/state/security-analyzer-slice";+import { ActionSecurityRisk } from "#/stores/security-analyzer-store";import {FileWriteAction,CommandAction,diff --git a/frontend/src/components/shared/buttons/confirmation-buttons.tsx b/frontend/src/components/shared/buttons/confirmation-buttons.tsxindex d15da502f5..5c9e2612de 100644--- a/frontend/src/components/shared/buttons/confirmation-buttons.tsx+++ b/frontend/src/components/shared/buttons/confirmation-buttons.tsx@@ -7,7 +7,7 @@ import { generateAgentStateChangeEvent } from "#/services/agent-state-service";import { useWsClient } from "#/context/ws-client-provider";import { ActionTooltip } from "../action-tooltip";import { isOpenHandsAction } from "#/types/core/guards";-import { ActionSecurityRisk } from "#/state/security-analyzer-slice";+import { ActionSecurityRisk } from "#/stores/security-analyzer-store";import { RiskAlert } from "#/components/shared/risk-alert";import WarningIcon from "#/icons/u-warning.svg?react";import { RootState } from "#/store";diff --git a/frontend/src/services/actions.ts b/frontend/src/services/actions.tsindex 53fed792a7..4ec1c20f12 100644--- a/frontend/src/services/actions.ts+++ b/frontend/src/services/actions.ts@@ -1,5 +1,4 @@import { trackError } from "#/utils/error-handler";-import { appendSecurityAnalyzerInput } from "#/state/security-analyzer-slice";import useMetricsStore from "#/stores/metrics-store";import { useStatusStore } from "#/state/status-store";import store from "#/store";@@ -13,6 +12,10 @@ import { handleObservationMessage } from "./observations";import { useCommandStore } from "#/state/command-store";import { appendJupyterInput } from "#/state/jupyter-slice";import { queryClient } from "#/query-client-config";+import {+ ActionSecurityRisk,+ useSecurityAnalyzerStore,+} from "#/stores/security-analyzer-store";export function handleActionMessage(message: ActionMessage) {if (message.args?.hidden) {@@ -38,7 +41,22 @@ export function handleActionMessage(message: ActionMessage) {}if ("args" in message && "security_risk" in message.args) {- store.dispatch(appendSecurityAnalyzerInput(message));+ useSecurityAnalyzerStore.getState().appendSecurityAnalyzerInput({+ id: message.id,+ args: {+ command: message.args.command,+ code: message.args.code,+ content: message.args.content,+ security_risk: message.args+ .security_risk as unknown as ActionSecurityRisk,+ confirmation_state: message.args.confirmation_state as+ | "awaiting_confirmation"+ | "confirmed"+ | "rejected"+ | undefined,+ },+ message: message.message,+ });}}diff --git a/frontend/src/state/security-analyzer-slice.ts b/frontend/src/state/security-analyzer-slice.tsdeleted file mode 100644index 22ecb09959..0000000000--- a/frontend/src/state/security-analyzer-slice.ts+++ /dev/null@@ -1,60 +0,0 @@-import { createSlice } from "@reduxjs/toolkit";--export enum ActionSecurityRisk {- UNKNOWN = -1,- LOW = 0,- MEDIUM = 1,- HIGH = 2,-}--export type SecurityAnalyzerLog = {- id: number;- content: string;- security_risk: ActionSecurityRisk;- confirmation_state?: "awaiting_confirmation" | "confirmed" | "rejected";- confirmed_changed: boolean;-};--const initialLogs: SecurityAnalyzerLog[] = [];--export const securityAnalyzerSlice = createSlice({- name: "securityAnalyzer",- initialState: {- logs: initialLogs,- },- reducers: {- appendSecurityAnalyzerInput: (state, action) => {- const log = {- id: action.payload.id,- content:- action.payload.args.command ||- action.payload.args.code ||- action.payload.args.content ||- action.payload.message,- security_risk: action.payload.args.security_risk as ActionSecurityRisk,- confirmation_state: action.payload.args.confirmation_state,- confirmed_changed: false,- };-- const existingLog = state.logs.find(- (stateLog) =>- stateLog.id === log.id ||- (stateLog.confirmation_state === "awaiting_confirmation" &&- stateLog.content === log.content),- );-- if (existingLog) {- if (existingLog.confirmation_state !== log.confirmation_state) {- existingLog.confirmation_state = log.confirmation_state;- existingLog.confirmed_changed = true;- }- } else {- state.logs.push(log);- }- },- },-});--export const { appendSecurityAnalyzerInput } = securityAnalyzerSlice.actions;--export default securityAnalyzerSlice.reducer;diff --git a/frontend/src/store.ts b/frontend/src/store.tsindex 7e75d76aa0..8c79ff2e0c 100644--- a/frontend/src/store.ts+++ b/frontend/src/store.ts@@ -2,7 +2,6 @@ import { combineReducers, configureStore } from "@reduxjs/toolkit";import agentReducer from "./state/agent-slice";import browserReducer from "./state/browser-slice";import { jupyterReducer } from "./state/jupyter-slice";-import securityAnalyzerReducer from "./state/security-analyzer-slice";import microagentManagementReducer from "./state/microagent-management-slice";import eventMessageReducer from "./state/event-message-slice";@@ -10,7 +9,6 @@ export const rootReducer = combineReducers({browser: browserReducer,agent: agentReducer,jupyter: jupyterReducer,- securityAnalyzer: securityAnalyzerReducer,microagentManagement: microagentManagementReducer,eventMessage: eventMessageReducer,});diff --git a/frontend/src/stores/security-analyzer-store.ts b/frontend/src/stores/security-analyzer-store.tsnew file mode 100644index 0000000000..a8522da813--- /dev/null+++ b/frontend/src/stores/security-analyzer-store.ts@@ -0,0 +1,75 @@+import { create } from "zustand";++export enum ActionSecurityRisk {+ UNKNOWN = -1,+ LOW = 0,+ MEDIUM = 1,+ HIGH = 2,+}++export type SecurityAnalyzerLog = {+ id: number;+ content: string;+ security_risk: ActionSecurityRisk;+ confirmation_state?: "awaiting_confirmation" | "confirmed" | "rejected";+ confirmed_changed: boolean;+};++interface SecurityAnalyzerState {+ logs: SecurityAnalyzerLog[];+}++interface SecurityAnalyzerStore extends SecurityAnalyzerState {+ appendSecurityAnalyzerInput: (message: {+ id: number;+ args: {+ command?: string;+ code?: string;+ content?: string;+ security_risk: ActionSecurityRisk;+ confirmation_state?: "awaiting_confirmation" | "confirmed" | "rejected";+ };+ message?: string;+ }) => void;+ clearLogs: () => void;+}++const initialLogs: SecurityAnalyzerLog[] = [];++export const useSecurityAnalyzerStore = create<SecurityAnalyzerStore>(+ (set) => ({+ logs: initialLogs,+ appendSecurityAnalyzerInput: (message) =>+ set((state) => {+ const log: SecurityAnalyzerLog = {+ id: message.id,+ content:+ message.args.command ||+ message.args.code ||+ message.args.content ||+ message.message ||+ "",+ security_risk: message.args.security_risk,+ confirmation_state: message.args.confirmation_state,+ confirmed_changed: false,+ };++ const existingLog = state.logs.find(+ (stateLog) =>+ stateLog.id === log.id ||+ (stateLog.confirmation_state === "awaiting_confirmation" &&+ stateLog.content === log.content),+ );++ if (existingLog) {+ if (existingLog.confirmation_state !== log.confirmation_state) {+ existingLog.confirmation_state = log.confirmation_state;+ existingLog.confirmed_changed = true;+ }+ return { logs: [...state.logs] }; // Return new array to trigger re-render+ }+ return { logs: [...state.logs, log] };+ }),+ clearLogs: () => set({ logs: initialLogs }),+ }),+);diff --git a/frontend/src/types/core/actions.ts b/frontend/src/types/core/actions.tsindex 380a17b1d4..89852f16e3 100644--- a/frontend/src/types/core/actions.ts+++ b/frontend/src/types/core/actions.ts@@ -1,5 +1,5 @@import { OpenHandsActionEvent } from "./base";-import { ActionSecurityRisk } from "#/state/security-analyzer-slice";+import { ActionSecurityRisk } from "#/stores/security-analyzer-store";export interface UserMessageAction extends OpenHandsActionEvent<"message"> {source: "user";