-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathterminal.ts
More file actions
71 lines (63 loc) · 2.38 KB
/
terminal.ts
File metadata and controls
71 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Terminal utilities for managing terminal sessions and windows.
*
* Consolidates common terminal operations used across:
* - useOpenTerminal hook (new pop-out terminals)
* - RightSidebar (integrated terminals, pop-out existing)
*/
import type { RouterClient } from "@orpc/server";
import type { AppRouter } from "@/node/orpc/router";
import { resolveBrowserAssetUrl } from "@/browser/utils/frontendBasePath";
type APIClient = RouterClient<AppRouter>;
/** Default terminal size used when creating sessions before the terminal is mounted */
export interface TerminalSessionCreateOptions {
/** Optional command to run immediately after terminal creation */
initialCommand?: string;
}
export const DEFAULT_TERMINAL_SIZE = { cols: 80, rows: 24 };
/**
* Open a terminal in a pop-out window.
*
* Handles both browser mode (window.open) and Electron mode (terminal.openWindow).
* In browser mode, opens the window client-side since the backend can't open windows.
* In Electron mode, the backend opens a BrowserWindow.
*
* @param api - The API client
* @param workspaceId - Workspace ID
* @param sessionId - Terminal session ID (required)
*/
export function openTerminalPopout(api: APIClient, workspaceId: string, sessionId: string): void {
const isBrowser = !window.api;
if (isBrowser) {
// In browser mode, we must open the window client-side
// The backend cannot open a window on the user's client
const params = new URLSearchParams({ workspaceId, sessionId });
const terminalUrl = resolveBrowserAssetUrl(`terminal.html?${params.toString()}`);
window.open(
terminalUrl,
`terminal-${workspaceId}-${Date.now()}`,
"width=1000,height=600,popup=yes"
);
}
// Open via backend (Electron pops up BrowserWindow, browser already opened above)
void api.terminal.openWindow({ workspaceId, sessionId });
}
/**
* Create a new terminal session with default size.
*
* @param api - The API client
* @param workspaceId - Workspace ID
* @returns The created session with sessionId
*/
export async function createTerminalSession(
api: APIClient,
workspaceId: string,
options?: TerminalSessionCreateOptions
): Promise<{ sessionId: string; workspaceId: string; cols: number; rows: number }> {
return api.terminal.create({
workspaceId,
cols: DEFAULT_TERMINAL_SIZE.cols,
rows: DEFAULT_TERMINAL_SIZE.rows,
initialCommand: options?.initialCommand,
});
}