Repository navigation
Expand file tree
/
Copy pathTerminalPanel.tsx
More file actions
253 lines (242 loc) · 7.63 KB
/
Copy pathTerminalPanel.tsx
File metadata and controls
253 lines (242 loc) · 7.63 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/** The drawer's Terminal tab: the user's own shells in this project, one
* per tab. They keep running when the drawer closes or another tab opens,
* work while the agent runs, and are never shown to the model. */
import {
Suspense,
lazy,
useCallback,
useEffect,
useRef,
useState,
} from "react";
import { Plus, X } from "lucide-react";
import { terminalApi, type TerminalInfo } from "../lib/terminal";
import { getRemoteSession } from "../lib/remote";
import { isRemote } from "../lib/transport";
import {
remembered,
type DrawerMemory,
type DrawerMemoryUpdate,
} from "../hooks/useDrawerMemory";
import "../tools.css";
type Toast = (text: string, kind?: "ok" | "err" | "info") => void;
/** xterm.js loads with the first terminal, not with the window. */
const TerminalView = lazy(() =>
import("./TerminalView").then((m) => ({ default: m.TerminalView })),
);
/** Rough initial size; the view fits itself once laid out. */
const START = { cols: 80, rows: 24 };
type PanelProps = {
workspace: string;
toast: Toast;
memory: DrawerMemory;
onMemory: DrawerMemoryUpdate;
onRequestFocus?: () => void;
onReady?: (element: HTMLElement) => void;
};
export function TerminalPanel(props: PanelProps) {
// Over remote access terminals stay off unless the desktop allows them.
if (isRemote() && !getRemoteSession()?.allow_terminals)
return (
<section className="terminal-panel" aria-label="Terminals">
<p className="hint terminal-hint">
Terminals are turned off for remote access. To use them from this
device, turn on “Allow terminals over remote access” in Settings ›
Remote access on the computer running ShadowCode.
</p>
</section>
);
return <LocalTerminals {...props} />;
}
function LocalTerminals({
workspace,
toast,
memory,
onMemory,
onRequestFocus,
onReady,
}: PanelProps) {
const [active, setActive] = remembered(memory, onMemory, "terminalActive");
const [terminals, setTerminals] = useState<TerminalInfo[] | null>(null);
const [error, setError] = useState("");
const [opening, setOpening] = useState(false);
const started = useRef(false);
const open = useCallback(async () => {
setOpening(true);
setError("");
try {
const made = await terminalApi.open(START.cols, START.rows);
setTerminals((list) => [...(list || []), made]);
onMemory("terminalActive", made.id);
} catch (e) {
setError(String(e));
} finally {
setOpening(false);
}
}, [onMemory]);
useEffect(() => {
let current = true;
void terminalApi
.list()
.then(async ({ terminals: list }) => {
if (!current) return;
setTerminals(list);
// The tab opens on a shell: start one when none is running here.
if (!list.length && !started.current) {
started.current = true;
await open();
}
})
.catch((e) => {
if (current) {
setTerminals([]);
setError(String(e));
}
});
return () => {
current = false;
};
}, [workspace, open]);
const list = terminals || [];
const shown = list.find((t) => t.id === active) || list[list.length - 1];
// Display only; retain the original workspace for identity and operations.
const projectName =
workspace.split(/[\\/]/).filter(Boolean).pop() || workspace;
async function close(id: string) {
try {
await terminalApi.close(id);
} catch (e) {
// Already gone in the engine: drop the tab anyway.
if (!String(e).includes("closed")) toast(String(e), "err");
}
setTerminals((rows) => (rows || []).filter((t) => t.id !== id));
if (active === id) setActive("");
}
return (
<section className="terminal-panel" aria-label="Terminals">
<div
className="terminal-controls"
role="region"
aria-label="Terminal controls and notices"
tabIndex={0}
>
<div className="terminal-tabs">
{list.map((t) => (
<div
key={t.id}
className={`terminal-tab ${shown?.id === t.id ? "on" : ""}`}
>
<button
type="button"
aria-pressed={shown?.id === t.id}
onClick={(event) => {
onRequestFocus?.();
if (shown?.id === t.id) {
const input = event.currentTarget
.closest(".terminal-panel")
?.querySelector<HTMLElement>(".xterm-helper-textarea");
if (input) onReady?.(input);
}
setActive(t.id);
}}
title={t.shell}
>
{t.title}
{t.exited && <span className="dim"> · exited</span>}
</button>
<button
type="button"
className="terminal-tab-close"
aria-label={`Close ${t.title}`}
onClick={() => void close(t.id)}
>
<X size={12} aria-hidden="true" />
</button>
</div>
))}
<button
type="button"
className="icon-btn terminal-new"
aria-label="New terminal"
title="New terminal"
disabled={opening}
onClick={() => {
onRequestFocus?.();
void open();
}}
>
<Plus size={14} aria-hidden="true" />
</button>
</div>
<p className="hint terminal-hint">
Your own shell in {projectName}. It runs outside the agent's sandbox,
keeps running while the agent works, and the agent never sees it.
</p>
{error && (
<p className="hint error" role="alert">
{error}
</p>
)}
{terminals === null && <p role="status">Starting a terminal…</p>}
{terminals !== null && !list.length && !opening && (
<div className="terminal-empty">
<p className="hint">No terminal is open in this project.</p>
<button
type="button"
className="mini"
onClick={() => {
onRequestFocus?.();
void open();
}}
>
Open a terminal
</button>
</div>
)}
{shown?.exited && (
<div className="terminal-exited" role="status">
<span>
The shell exited
{shown.exit_code !== null ? ` with code ${shown.exit_code}` : ""}.
</span>
<button
type="button"
className="mini"
onClick={() => {
onRequestFocus?.();
void close(shown.id).then(open);
}}
>
Restart
</button>
</div>
)}
</div>
{shown && (
<Suspense fallback={<p role="status">Starting a terminal…</p>}>
<TerminalView
key={shown.id}
id={shown.id}
onReady={onReady}
title={shown.title}
onError={(message) => setError(message)}
onState={(chunk) => {
if (chunk.exited !== shown.exited)
setTerminals((rows) =>
(rows || []).map((t) =>
t.id === chunk.id
? {
...t,
exited: chunk.exited,
exit_code: chunk.exit_code,
}
: t,
),
);
}}
/>
</Suspense>
)}
</section>
);
}