Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/browser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ function parseWorkspaceActivity(value: unknown): WorkspaceActivitySnapshot | nul
};
}

// Fire-and-forget helper for IPC calls that don't need a response
// Uses fetch with keepalive to ensure the request completes even if page unloads
function sendIPCFireAndForget(channel: string, ...args: unknown[]): void {
// Don't await - fire and forget
void fetch(`${API_BASE}/ipc/${encodeURIComponent(channel)}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ args }),
keepalive: true, // Ensures request completes even on page unload
}).catch((err) => {
console.error(`Fire-and-forget IPC error (${channel}):`, err);
});
}
// WebSocket connection manager
class WebSocketManager {
private ws: WebSocket | null = null;
Expand Down Expand Up @@ -337,8 +352,8 @@ const webApi: IPCApi = {
close: (sessionId) => invokeIPC(IPC_CHANNELS.TERMINAL_CLOSE, sessionId),
resize: (params) => invokeIPC(IPC_CHANNELS.TERMINAL_RESIZE, params),
sendInput: (sessionId: string, data: string) => {
// Send via IPC - in browser mode this becomes an HTTP POST
void invokeIPC(IPC_CHANNELS.TERMINAL_INPUT, sessionId, data);
// Fire-and-forget for minimal latency - no need to wait for response
sendIPCFireAndForget(IPC_CHANNELS.TERMINAL_INPUT, sessionId, data);
},
onOutput: (sessionId: string, callback: (data: string) => void) => {
// Subscribe to terminal output events via WebSocket
Expand Down
19 changes: 19 additions & 0 deletions src/cli/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ class HttpIpcMainAdapter {
on(channel: string, handler: (event: unknown, ...args: unknown[]) => void): void {
if (!this.listeners.has(channel)) {
this.listeners.set(channel, []);
// Register HTTP route for fire-and-forget handlers too
// Unlike handle(), we don't wait for or return any result
this.app.post(`/ipc/${encodeURIComponent(channel)}`, (req, res) => {
try {
const schema = z.object({ args: z.array(z.unknown()).optional() });
const body = schema.parse(req.body);
const args: unknown[] = body.args ?? [];
// Fire-and-forget: call all listeners, respond immediately
const listeners = this.listeners.get(channel);
if (listeners) {
listeners.forEach((listener) => listener(null, ...args));
}
res.json({ success: true });
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Error in fire-and-forget handler ${channel}:`, error);
res.json({ success: false, error: message });
}
});
}
this.listeners.get(channel)!.push(handler);
}
Expand Down
4 changes: 3 additions & 1 deletion src/desktop/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ const api: IPCApi = {
close: (sessionId) => ipcRenderer.invoke(IPC_CHANNELS.TERMINAL_CLOSE, sessionId),
resize: (params) => ipcRenderer.invoke(IPC_CHANNELS.TERMINAL_RESIZE, params),
sendInput: (sessionId: string, data: string) => {
void ipcRenderer.invoke(IPC_CHANNELS.TERMINAL_INPUT, sessionId, data);
// Use send() instead of invoke() for fire-and-forget - no need to wait for response
// This reduces input latency significantly for fast typing
ipcRenderer.send(IPC_CHANNELS.TERMINAL_INPUT, sessionId, data);
},
onOutput: (sessionId: string, callback: (data: string) => void) => {
const channel = `terminal:output:${sessionId}`;
Expand Down
6 changes: 3 additions & 3 deletions src/node/services/ipcMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1877,13 +1877,13 @@ export class IpcMain {
});

// Handle terminal input (keyboard, etc.)
// Use handle() for both Electron and browser mode
ipcMain.handle(IPC_CHANNELS.TERMINAL_INPUT, (_event, sessionId: string, data: string) => {
// Use on() instead of handle() for fire-and-forget - reduces input latency
ipcMain.on(IPC_CHANNELS.TERMINAL_INPUT, (_event, sessionId: string, data: string) => {
try {
this.ptyService.sendInput(sessionId, data);
} catch (err) {
log.error(`Error sending input to terminal ${sessionId}:`, err);
throw err;
// No throw - fire-and-forget doesn't return errors to caller
}
});

Expand Down