From 389a1cbdc8b1124a757c590972d9c09fe6104376 Mon Sep 17 00:00:00 2001 From: Rohit Date: Wed, 9 Apr 2025 19:40:48 +0530 Subject: [PATCH] feat: add robot retraining logic --- src/components/robot/RecordingsTable.tsx | 85 +++++++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/src/components/robot/RecordingsTable.tsx b/src/components/robot/RecordingsTable.tsx index 2fc4f26e..878c998f 100644 --- a/src/components/robot/RecordingsTable.tsx +++ b/src/components/robot/RecordingsTable.tsx @@ -35,7 +35,8 @@ import { Settings, Power, ContentCopy, - MoreHoriz + MoreHoriz, + Refresh } from "@mui/icons-material"; import { useGlobalInfoStore } from "../../context/globalInfo"; import { checkRunsForRecording, deleteRecordingFromStorage, getStoredRecordings } from "../../api/storage"; @@ -117,6 +118,7 @@ const TableRowMemoized = memo(({ row, columns, handlers }: any) => { return ( handlers.handleRetrainRobot(row.id, row.name)} handleEdit={() => handlers.handleEditRobot(row.id, row.name, row.params || [])} handleDuplicate={() => handlers.handleDuplicateRobot(row.id, row.name, row.params || [])} handleDelete={() => handlers.handleDelete(row.id)} @@ -198,6 +200,17 @@ export const RecordingsTable = ({ } } } + + if (event.data && event.data.type === 'session-data-clear') { + window.sessionStorage.removeItem('browserId'); + window.sessionStorage.removeItem('robotToRetrain'); + window.sessionStorage.removeItem('robotName'); + window.sessionStorage.removeItem('recordingUrl'); + window.sessionStorage.removeItem('recordingSessionId'); + window.sessionStorage.removeItem('pendingSessionData'); + window.sessionStorage.removeItem('nextTabIsRecording'); + window.sessionStorage.removeItem('initialUrl'); + } }; window.addEventListener('message', handleMessage); @@ -303,6 +316,63 @@ export const RecordingsTable = ({ setModalOpen(true); }; + const handleRetrainRobot = useCallback(async (id: string, name: string) => { + const activeBrowserId = await getActiveBrowserId(); + const robot = rows.find(row => row.id === id); + let targetUrl; + + if (robot?.content?.workflow && robot.content.workflow.length > 0) { + // Get the last workflow item + const lastPair = robot.content.workflow[robot.content.workflow.length - 1]; + + if (lastPair?.what) { + if (Array.isArray(lastPair.what)) { + const gotoAction = lastPair.what.find(action => + action && typeof action === 'object' && 'action' in action && action.action === "goto" + ) as any; + + if (gotoAction?.args?.[0]) { + targetUrl = gotoAction.args[0]; + } + } + } + } + + // Set the URL in state and session storage + if (targetUrl) { + setInitialUrl(targetUrl); + setRecordingUrl(targetUrl); + window.sessionStorage.setItem('initialUrl', targetUrl); + } + + if (activeBrowserId) { + setActiveBrowserId(activeBrowserId); + setWarningModalOpen(true); + } else { + // Pass the URL directly to avoid timing issues with state updates + startRetrainRecording(id, name, targetUrl); + } + }, [rows, setInitialUrl, setRecordingUrl]); + + const startRetrainRecording = (id: string, name: string, url?: string) => { + setBrowserId('new-recording'); + setRecordingName(''); + setRecordingId(''); + + window.sessionStorage.setItem('browserId', 'new-recording'); + window.sessionStorage.setItem('robotToRetrain', id); + window.sessionStorage.setItem('robotName', name); + + window.sessionStorage.setItem('recordingUrl', url || recordingUrl); + + const sessionId = Date.now().toString(); + window.sessionStorage.setItem('recordingSessionId', sessionId); + + window.openedRecordingWindow = window.open(`/recording-setup?session=${sessionId}`, '_blank'); + + window.sessionStorage.setItem('nextTabIsRecording', 'true'); + }; + const startRecording = () => { setModalOpen(false); @@ -381,6 +451,7 @@ export const RecordingsTable = ({ handleSettingsRecording, handleEditRobot, handleDuplicateRobot, + handleRetrainRobot, handleDelete: async (id: string) => { const hasRuns = await checkRunsForRecording(id); if (hasRuns) { @@ -395,7 +466,7 @@ export const RecordingsTable = ({ fetchRecordings(); } } - }), [handleRunRecording, handleScheduleRecording, handleIntegrateRecording, handleSettingsRecording, handleEditRobot, handleDuplicateRobot, notify, t]); + }), [handleRunRecording, handleScheduleRecording, handleIntegrateRecording, handleSettingsRecording, handleEditRobot, handleDuplicateRobot, handleRetrainRobot, notify, t]); return ( @@ -597,12 +668,13 @@ const SettingsButton = ({ handleSettings }: SettingsButtonProps) => { } interface OptionsButtonProps { + handleRetrain: () => void; handleEdit: () => void; handleDelete: () => void; handleDuplicate: () => void; } -const OptionsButton = ({ handleEdit, handleDelete, handleDuplicate }: OptionsButtonProps) => { +const OptionsButton = ({ handleRetrain, handleEdit, handleDelete, handleDuplicate }: OptionsButtonProps) => { const [anchorEl, setAnchorEl] = React.useState(null); const handleClick = (event: React.MouseEvent) => { @@ -629,6 +701,13 @@ const OptionsButton = ({ handleEdit, handleDelete, handleDuplicate }: OptionsBut open={Boolean(anchorEl)} onClose={handleClose} > + { handleRetrain(); handleClose(); }}> + + + + {t('recordingtable.retrain')} + + { handleEdit(); handleClose(); }}>