Skip to content
Closed
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
1 change: 1 addition & 0 deletions sandbox-agent/sdks/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"sandbox-agent": "^0.2.2"
},
"dependencies": {
"@openclaw/libterminal": "0.4.0",
"@tanstack/react-virtual": "^3.13.22",
"ghostty-web": "^0.4.0"
},
Expand Down
54 changes: 30 additions & 24 deletions sandbox-agent/sdks/react/src/ProcessTerminal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import type { FitAddon as GhosttyFitAddon, Terminal as GhosttyTerminal } from "ghostty-web";
import type { GhosttyTerminalController } from "@openclaw/libterminal/browser";
import type { CSSProperties } from "react";
import { useEffect, useRef, useState } from "react";
import type { SandboxAgent, TerminalErrorStatus, TerminalExitStatus, TerminalReadyStatus } from "sandbox-agent";
Expand Down Expand Up @@ -111,8 +111,8 @@ export const ProcessTerminal = ({

useEffect(() => {
let cancelled = false;
let terminal: GhosttyTerminal | null = null;
let fitAddon: GhosttyFitAddon | null = null;
const abortController = new AbortController();
let controller: GhosttyTerminalController | null = null;
let session: ReturnType<ProcessTerminalClient["connectProcessTerminal"]> | null = null;
let resizeRaf = 0;
let removeDataListener: { dispose(): void } | null = null;
Expand All @@ -123,45 +123,51 @@ export const ProcessTerminal = ({
setExitCode(null);

const syncSize = () => {
if (!terminal || !session) {
if (!controller || !session) {
return;
}

session.resize({
cols: terminal.cols,
rows: terminal.rows,
cols: controller.terminal.cols,
rows: controller.terminal.rows,
});
};

const connect = async () => {
try {
const ghostty = await import("ghostty-web");
await ghostty.init();
const { createGhosttyTerminal } = await import("@openclaw/libterminal/browser");

if (cancelled || !hostRef.current) {
return;
}

terminal = new ghostty.Terminal({
allowTransparency: true,
cursorBlink: true,
cursorStyle: "block",
fontFamily: "ui-monospace, SFMono-Regular, SF Mono, Menlo, monospace",
fontSize: 13,
smoothScrollDuration: 90,
theme: terminalTheme,
controller = await createGhosttyTerminal({
parent: hostRef.current,
readOnly: false,
signal: abortController.signal,
// Keep the initial dimensions until the canvas styles are applied below.
size: { columns: 80, rows: 24 },
terminalOptions: {
allowTransparency: true,
cursorBlink: true,
cursorStyle: "block",
fontFamily: "ui-monospace, SFMono-Regular, SF Mono, Menlo, monospace",
fontSize: 13,
smoothScrollDuration: 90,
theme: terminalTheme,
},
});
fitAddon = new ghostty.FitAddon();
if (cancelled) {
return;
}

terminal.open(hostRef.current);
const terminal = controller.terminal;
const terminalRoot = hostRef.current.firstElementChild;
if (terminalRoot instanceof HTMLElement) {
terminalRoot.style.width = "100%";
terminalRoot.style.height = "100%";
}
terminal.loadAddon(fitAddon);
fitAddon.fit();
fitAddon.observeResize();
controller.fit();
terminal.focus();

removeDataListener = terminal.onData((data) => {
Expand Down Expand Up @@ -189,10 +195,10 @@ export const ProcessTerminal = ({
});

nextSession.onData((bytes: Uint8Array) => {
if (cancelled || !terminal) {
if (cancelled || !controller) {
return;
}
terminal.write(bytes);
controller.write(bytes);
});

nextSession.onExit((frame: TerminalExitStatus) => {
Expand Down Expand Up @@ -246,7 +252,7 @@ export const ProcessTerminal = ({
removeDataListener?.dispose();
removeResizeListener?.dispose();
session?.close();
terminal?.dispose();
abortController.abort();
};
}, [client, onError, onExit, processId]);

Expand Down