IPC Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The Dyad application utilizes Electron's Inter-Process Communication (IPC) to establish a secure boundary between the frontend renderer process (React app) and the backend main process. This architecture ensures that privileged operations and direct system access are confined to the main process, while the renderer process communicates through a well-defined and restricted API.
This separation is a critical security practice in Electron applications. The frontend can only invoke functions explicitly exposed by the main process, preventing potentially malicious client-side code from gaining unintended access to system resources.
Sources: AGENTS.md
Core Components
The IPC system is built around three main components that work together to facilitate secure communication.
| Component | Location | Description |
|---|---|---|
IpcHost | Main Process | A singleton class that runs in the main process. It is responsible for registering and managing all available IPC handlers. |
IpcClient | Renderer Process | A singleton class that runs in the renderer process. It provides a type-safe API for the React frontend to invoke IPC endpoints. |
preload.ts | Bridge | An Electron preload script that acts as a secure bridge. It defines an allowlist of IPC channels that the renderer is permitted to access. |
Sources: AGENTS.md:10-15, src/ipc/ipc_host.ts, src/ipc/ipc_client.ts
Architectural Overview
The following diagram illustrates the high-level communication flow between the renderer and main processes.
This diagram shows a request originating from a React component, passing through the IpcClient and preload.ts script to the IpcHost, which then delegates to the appropriate handler.
Sources: AGENTS.md:10-15
Communication Flow
A typical IPC call follows a request-response pattern managed by Electron's ipcMain.handle and ipcRenderer.invoke methods.
Sequence Diagram
This sequence diagram details the steps involved in a single IPC call from the frontend to the backend and back.
Sources: src/ipc/ipc_client.ts, AGENTS.md:10-15
IPC Host
The IpcHost is the central hub for all incoming IPC requests in the main process. It uses a singleton pattern to ensure a single instance manages all handlers.
Handler Registration
Handlers are organized into separate files (e.g., app_handlers.ts, chat_stream_handlers.ts) and are registered with the IpcHost instance. The host provides dedicated methods for registering different groups of handlers.
// src/ipc/ipc_host.ts:31-42
public registerAppHandlers(window: BrowserWindow) {
registerAppHandlers(this.safeHandle, window);
}
public registerChatStreamHandlers(dyadProcess: DyadProcess) {
registerChatStreamHandlers(this.safeHandle, dyadProcess);
}
public registerSettingsHandlers() {
registerSettingsHandlers(this.safeHandle);
}
// ... and so on
Each registration function passes the safeHandle method to the specific handler modules.
Sources: src/ipc/ipc_host.ts:31-42, AGENTS.md:14-15
Error Handling with safeHandle
To ensure consistency and security, all IPC handlers are wrapped by the safeHandle method. This wrapper standardizes error handling.
- On Success: It returns a
IpcResultobject with{ success: true, data: ... }. - On Failure: It catches any thrown errors, logs them, and returns
{ success: false, error: ... }.
This approach prevents unhandled exceptions in the main process from crashing the application and provides a consistent error structure to the client. The recommendation is for handlers to throw new Error(...) on failure, which safeHandle will then catch.
// src/ipc/handlers/safe_handle.ts:11-25
export const safeHandle =
(logger: log.LogFunctions) =>
<T>(
channel: IpcChannel,
handler: (...args: any[]) => Promise<T>,
): void => {
ipcMain.handle(channel, async (...args) => {
try {
const result = await handler(...args);
return { success: true, data: result };
} catch (e) {
logger.error(`Error in IPC handler for channel ${channel}:`, e);
return {
success: false,
error: e instanceof Error ? e.message : String(e),
};
}
});
};
Sources: src/ipc/handlers/safe_handle.ts:11-25, AGENTS.md:16-17
IPC Client
The IpcClient provides a developer-friendly interface in the renderer process for making IPC calls. It is also a singleton.
Invoking Endpoints
The client exposes dedicated, type-safe methods for each IPC channel. These methods wrap the generic invoke call, providing better autocompletion and type-checking.
// src/ipc/ipc_client.ts:27-33
private async invoke<T>(channel: IpcChannel, ...args: any[]): Promise<T> {
const result: IpcResult<T> =
await window.dyadApi.invoke<T>(channel, ...args);
if (!result.success) {
throw new Error(result.error);
}
If the IpcResult from the host indicates a failure (success: false), the client method throws an error, which can be caught by the calling code, such as a TanStack Query mutation.
Sources: src/ipc/ipc_client.ts:27-33, AGENTS.md:18-21
React + TanStack Query Integration
The project standardizes on using TanStack Query to interact with the IPC layer from React components. This pattern simplifies data fetching, caching, and state management.
- Reads (
useQuery): Operations that fetch data should be wrapped inuseQuery. ThequeryFnwill call the relevantIpcClientmethod. - Writes (
useMutation): Operations that create, update, or delete data should be wrapped inuseMutation. The mutation function calls theIpcClientmethod, and related queries are invalidated upon success.
Sources: AGENTS.md:23-27
Integration Flow
This diagram shows how TanStack Query hooks interface with the IpcClient to communicate over IPC channels.
Sources: AGENTS.md:23-27
Testing IPC Handlers
Unit tests for IPC handlers are written using Vitest. The testing setup requires mocking the electron module, particularly ipcMain, to isolate the handler logic from the Electron runtime.
Example mock for ipcMain:
vi.mock("electron", () => ({
ipcMain: {
handle: vi.fn(),
on: vi.fn(),
},
}));
This allows tests to verify that handlers are registered correctly and behave as expected when invoked.
Sources: src/tests/README.md:30-36