refactor: restructure to monorepo with apps and packages directories - Move companion app to apps/companion with electron-vite - Move domain/application/infrastructure to packages/ - Fix ELECTRON_RUN_AS_NODE env var issue for VS Code terminal - Remove legacy esbuild bundler (replaced by electron-vite) - Update workspace scripts in root package.json
This commit is contained in:
111
apps/companion/main/ipc-handlers.ts
Normal file
111
apps/companion/main/ipc-handlers.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
import { ipcMain } from 'electron';
|
||||
import type { BrowserWindow, IpcMainInvokeEvent } from 'electron';
|
||||
import { DIContainer } from './di-container';
|
||||
import type { HostedSessionConfig } from '@/packages/domain/entities/HostedSessionConfig';
|
||||
import { StepId } from '@/packages/domain/value-objects/StepId';
|
||||
import { MockAutomationEngineAdapter } from '@/packages/infrastructure/adapters/automation/MockAutomationEngineAdapter';
|
||||
|
||||
export function setupIpcHandlers(mainWindow: BrowserWindow): void {
|
||||
const container = DIContainer.getInstance();
|
||||
const startAutomationUseCase = container.getStartAutomationUseCase();
|
||||
const sessionRepository = container.getSessionRepository();
|
||||
const automationEngine = container.getAutomationEngine();
|
||||
|
||||
ipcMain.handle('start-automation', async (_event: IpcMainInvokeEvent, config: HostedSessionConfig) => {
|
||||
try {
|
||||
// Connect to browser first (required for dev mode)
|
||||
const connectionResult = await container.initializeBrowserConnection();
|
||||
if (!connectionResult.success) {
|
||||
return { success: false, error: connectionResult.error };
|
||||
}
|
||||
|
||||
const result = await startAutomationUseCase.execute(config);
|
||||
const session = await sessionRepository.findById(result.sessionId);
|
||||
|
||||
if (session) {
|
||||
// Start the automation by executing step 1
|
||||
await automationEngine.executeStep(StepId.create(1), config);
|
||||
}
|
||||
|
||||
// Set up progress monitoring
|
||||
const checkInterval = setInterval(async () => {
|
||||
const updatedSession = await sessionRepository.findById(result.sessionId);
|
||||
if (!updatedSession) {
|
||||
clearInterval(checkInterval);
|
||||
return;
|
||||
}
|
||||
|
||||
mainWindow.webContents.send('session-progress', {
|
||||
sessionId: result.sessionId,
|
||||
currentStep: updatedSession.currentStep.value,
|
||||
state: updatedSession.state.value,
|
||||
completedSteps: Array.from({ length: updatedSession.currentStep.value - 1 }, (_, i) => i + 1),
|
||||
hasError: updatedSession.errorMessage !== undefined,
|
||||
errorMessage: updatedSession.errorMessage || null
|
||||
});
|
||||
|
||||
if (updatedSession.state.value === 'COMPLETED' ||
|
||||
updatedSession.state.value === 'FAILED' ||
|
||||
updatedSession.state.value === 'STOPPED_AT_STEP_18') {
|
||||
clearInterval(checkInterval);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
sessionId: result.sessionId
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('get-session-status', async (_event: IpcMainInvokeEvent, sessionId: string) => {
|
||||
const session = await sessionRepository.findById(sessionId);
|
||||
if (!session) {
|
||||
return { found: false };
|
||||
}
|
||||
|
||||
return {
|
||||
found: true,
|
||||
currentStep: session.currentStep.value,
|
||||
state: session.state.value,
|
||||
completedSteps: Array.from({ length: session.currentStep.value - 1 }, (_, i) => i + 1),
|
||||
hasError: session.errorMessage !== undefined,
|
||||
errorMessage: session.errorMessage || null
|
||||
};
|
||||
});
|
||||
|
||||
ipcMain.handle('pause-automation', async (_event: IpcMainInvokeEvent, _sessionId: string) => {
|
||||
return { success: false, error: 'Pause not implemented in POC' };
|
||||
});
|
||||
|
||||
ipcMain.handle('resume-automation', async (_event: IpcMainInvokeEvent, _sessionId: string) => {
|
||||
return { success: false, error: 'Resume not implemented in POC' };
|
||||
});
|
||||
|
||||
ipcMain.handle('stop-automation', async (_event: IpcMainInvokeEvent, sessionId: string) => {
|
||||
try {
|
||||
// Stop the automation engine interval
|
||||
const engine = automationEngine as MockAutomationEngineAdapter;
|
||||
engine.stopAutomation();
|
||||
|
||||
// Update session state to failed with user stop reason
|
||||
const session = await sessionRepository.findById(sessionId);
|
||||
if (session) {
|
||||
session.fail('User stopped automation');
|
||||
await sessionRepository.update(session);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user