shellpuppet 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +210 -33
- package/dist/cli/desktopShortcut.d.ts +22 -0
- package/dist/cli/desktopShortcut.js +157 -0
- package/dist/cli/desktopShortcut.js.map +1 -0
- package/dist/cli/index.js +268 -59
- package/dist/cli/index.js.map +1 -1
- package/dist/client/http.d.ts +74 -3
- package/dist/client/http.js +560 -37
- package/dist/client/http.js.map +1 -1
- package/dist/core/dialogs.d.ts +20 -12
- package/dist/core/dialogs.js +31 -19
- package/dist/core/dialogs.js.map +1 -1
- package/dist/core/manager.d.ts +35 -5
- package/dist/core/manager.js +121 -8
- package/dist/core/manager.js.map +1 -1
- package/dist/core/nativePty.d.ts +9 -5
- package/dist/core/nativePty.js +11 -6
- package/dist/core/nativePty.js.map +1 -1
- package/dist/core/promptDetector.d.ts +37 -3
- package/dist/core/promptDetector.js +303 -12
- package/dist/core/promptDetector.js.map +1 -1
- package/dist/core/reasoning.d.ts +19 -6
- package/dist/core/reasoning.js +223 -30
- package/dist/core/reasoning.js.map +1 -1
- package/dist/core/regexBudget.d.ts +20 -0
- package/dist/core/regexBudget.js +25 -1
- package/dist/core/regexBudget.js.map +1 -1
- package/dist/core/renderer.d.ts +16 -1
- package/dist/core/renderer.js +97 -6
- package/dist/core/renderer.js.map +1 -1
- package/dist/core/selection.d.ts +6 -0
- package/dist/core/selection.js +48 -0
- package/dist/core/selection.js.map +1 -0
- package/dist/core/session.d.ts +267 -13
- package/dist/core/session.js +585 -101
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +53 -18
- package/dist/core/types.js +30 -6
- package/dist/core/types.js.map +1 -1
- package/dist/core/waiter.d.ts +12 -4
- package/dist/core/waiter.js +63 -70
- package/dist/core/waiter.js.map +1 -1
- package/dist/core/windowsConsoleHost.d.ts +147 -0
- package/dist/core/windowsConsoleHost.js +588 -0
- package/dist/core/windowsConsoleHost.js.map +1 -0
- package/dist/core/windowsPtyStart.d.ts +100 -29
- package/dist/core/windowsPtyStart.js +143 -36
- package/dist/core/windowsPtyStart.js.map +1 -1
- package/dist/daemon/auth.d.ts +72 -2
- package/dist/daemon/auth.js +128 -3
- package/dist/daemon/auth.js.map +1 -1
- package/dist/daemon/config.d.ts +44 -1
- package/dist/daemon/config.js +276 -9
- package/dist/daemon/config.js.map +1 -1
- package/dist/daemon/folderPicker.d.ts +27 -0
- package/dist/daemon/folderPicker.js +115 -0
- package/dist/daemon/folderPicker.js.map +1 -0
- package/dist/daemon/index.js +20 -19
- package/dist/daemon/index.js.map +1 -1
- package/dist/daemon/launchProfiles.d.ts +155 -0
- package/dist/daemon/launchProfiles.js +554 -0
- package/dist/daemon/launchProfiles.js.map +1 -0
- package/dist/daemon/liveness.d.ts +1 -1
- package/dist/daemon/liveness.js +2 -2
- package/dist/daemon/liveness.js.map +1 -1
- package/dist/daemon/paths.d.ts +4 -0
- package/dist/daemon/paths.js.map +1 -1
- package/dist/daemon/restartHold.d.ts +16 -0
- package/dist/daemon/restartHold.js +126 -0
- package/dist/daemon/restartHold.js.map +1 -0
- package/dist/daemon/screenshot.d.ts +58 -1
- package/dist/daemon/screenshot.js +172 -26
- package/dist/daemon/screenshot.js.map +1 -1
- package/dist/daemon/server.d.ts +220 -10
- package/dist/daemon/server.js +1975 -205
- package/dist/daemon/server.js.map +1 -1
- package/dist/daemon/sessionFeed.d.ts +48 -0
- package/dist/daemon/sessionFeed.js +101 -0
- package/dist/daemon/sessionFeed.js.map +1 -0
- package/dist/daemon/viewerLauncher.d.ts +100 -9
- package/dist/daemon/viewerLauncher.js +349 -67
- package/dist/daemon/viewerLauncher.js.map +1 -1
- package/dist/mcp/config.d.ts +7 -14
- package/dist/mcp/config.js +10 -21
- package/dist/mcp/config.js.map +1 -1
- package/dist/mcp/index.js +20 -11
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/tools.d.ts +14 -7
- package/dist/mcp/tools.js +161 -60
- package/dist/mcp/tools.js.map +1 -1
- package/dist/viewer/dashboard-folders.js +20 -0
- package/dist/viewer/dashboard-layout.js +52 -0
- package/dist/viewer/dashboard.css +67 -0
- package/dist/viewer/dashboard.html +44 -0
- package/dist/viewer/dashboard.js +1523 -0
- package/dist/viewer/icon.ico +0 -0
- package/dist/viewer/index.html +12 -7
- package/dist/viewer/pane.js +736 -0
- package/dist/viewer/viewer.css +18 -7
- package/dist/viewer/viewer.js +47 -624
- package/package.json +13 -13
package/dist/viewer/viewer.js
CHANGED
|
@@ -1,86 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ShellPuppet viewer: the human's window into a shared session (design §5).
|
|
3
|
-
*
|
|
4
|
-
* One page, one session: the credential this page was given is scoped to
|
|
5
|
-
* exactly this session (design §4.1), so there is no session picker by design.
|
|
6
|
-
*
|
|
7
|
-
* Wire protocol: BINARY WebSocket frames carry terminal bytes (snapshots, the
|
|
8
|
-
* live stream and the exit notice); TEXT frames are JSON control messages:
|
|
9
|
-
* `identity`, `geometry`, `input-source`, `input-refused`, `exited`,
|
|
10
|
-
* `prompt-stall` and `prompt-clear`. What this page sends is text: a JSON
|
|
11
|
-
* resize message, or bytes for the program, which are either keystrokes or
|
|
12
|
-
* the terminal's own answers to the program's queries. The daemon tells those
|
|
13
|
-
* two apart.
|
|
14
|
-
*
|
|
15
|
-
* Three rules shape the page:
|
|
16
|
-
* - `term.onData` is registered exactly ONCE, never inside the connect path.
|
|
17
|
-
* Registering per-connect would stack a listener per reconnect and send
|
|
18
|
-
* every keystroke N times.
|
|
19
|
-
* - The fit addon measures what grid this window would like, and the page asks
|
|
20
|
-
* the daemon for it rather than assuming it (§5). The daemon decides who is
|
|
21
|
-
* driving the grid and answers with a geometry frame; only that frame
|
|
22
|
-
* resizes anything here.
|
|
23
|
-
* - The page never rebuilds terminal state itself. On connect and after every
|
|
24
|
-
* grid change the daemon sends a snapshot of its own renderer, a wipe followed
|
|
25
|
-
* by the bytes that rebuild the screen, so what this page shows is what the
|
|
26
|
-
* daemon renders, which is what the agent reads. A tail of the raw stream
|
|
27
|
-
* replayed here would start mid-frame.
|
|
28
|
-
*/
|
|
1
|
+
/** The single-session viewer page: grant URL, page controls and layout. */
|
|
29
2
|
|
|
30
|
-
/* global
|
|
3
|
+
/* global ShellPuppetPane */
|
|
31
4
|
|
|
32
5
|
(() => {
|
|
33
6
|
'use strict';
|
|
34
7
|
|
|
35
8
|
const params = new URLSearchParams(location.search);
|
|
36
|
-
//
|
|
37
|
-
// path: Chrome keys an --app window's AppUserModelID on host + path, and
|
|
38
|
-
// Windows groups taskbar buttons by that id, so a per-session path would
|
|
39
|
-
// give each window its own taskbar icon. A session id as the path's last
|
|
40
|
-
// segment (`/view/<id>`) is read too.
|
|
9
|
+
// A session id in the path is accepted as well as the query form.
|
|
41
10
|
const sessionId = params.get('s') || location.pathname.split('/')[2] || '';
|
|
42
|
-
|
|
43
|
-
// Capture mode (a headless screenshot, design §5): no chrome, the grid held
|
|
44
|
-
// at the daemon's geometry, never a resize sent and a flag raised once the
|
|
45
|
-
// first snapshot has been drawn, for the capturer to wait on. A screenshot
|
|
46
|
-
// must not change what the program sees.
|
|
47
11
|
const captureMode = params.get('capture') === '1';
|
|
12
|
+
const redactedCapture = captureMode && params.get('redact') === '1';
|
|
48
13
|
if (captureMode) document.body.classList.add('capture');
|
|
49
|
-
let firstFrameSeen = false;
|
|
50
14
|
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
// already redeemed it for an HttpOnly cookie that scripts cannot read and
|
|
54
|
-
// the browser attaches to the socket by itself. So there is nothing to keep
|
|
55
|
-
// here: only the spent grant to wipe from the address bar, so it never
|
|
56
|
-
// reaches history or a bookmark. The session id stays: without it a reload
|
|
57
|
-
// has nothing to reconnect to.
|
|
15
|
+
// The grant has already been redeemed for an HttpOnly cookie. Keep the
|
|
16
|
+
// session id for reloads while removing the spent grant from the URL.
|
|
58
17
|
history.replaceState(null, '', `/view?s=${encodeURIComponent(sessionId)}`);
|
|
59
18
|
|
|
60
|
-
const dot = document.getElementById('dot');
|
|
61
19
|
const banner = document.getElementById('banner');
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const zoomOutButton = document.getElementById('zoom-out');
|
|
69
|
-
const zoomAutoButton = document.getElementById('zoom-auto');
|
|
20
|
+
document.getElementById('session-label').textContent = sessionId;
|
|
21
|
+
if (!sessionId) {
|
|
22
|
+
banner.textContent = 'This viewer link is missing its session id. Ask the agent for a fresh viewer URL.';
|
|
23
|
+
banner.hidden = false;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
70
26
|
|
|
71
27
|
const MIN_FONT_PX = 6;
|
|
72
28
|
const MAX_FONT_PX = 40;
|
|
73
|
-
const DEFAULT_FONT_PX = 13;
|
|
74
29
|
const FONT_PREF_KEY = 'shellpuppet.fontSize';
|
|
75
|
-
|
|
76
|
-
function clampFont(px) {
|
|
77
|
-
return Math.min(MAX_FONT_PX, Math.max(MIN_FONT_PX, px));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// null = choose the text size automatically.
|
|
81
|
-
// A number = the human picked a size, and it wins until they press "fit".
|
|
82
|
-
// Persisted, because this viewer reconnects routinely and a preference that
|
|
83
|
-
// evaporated on every reconnect would be worse than none.
|
|
30
|
+
const clampFont = (px) => Math.min(MAX_FONT_PX, Math.max(MIN_FONT_PX, px));
|
|
84
31
|
let fontOverride = null;
|
|
85
32
|
try {
|
|
86
33
|
const saved = window.localStorage.getItem(FONT_PREF_KEY);
|
|
@@ -89,573 +36,49 @@
|
|
|
89
36
|
if (Number.isFinite(parsed)) fontOverride = clampFont(parsed);
|
|
90
37
|
}
|
|
91
38
|
} catch {
|
|
92
|
-
//
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
sessionLabel.textContent = sessionId;
|
|
96
|
-
|
|
97
|
-
const showBanner = (message) => {
|
|
98
|
-
banner.textContent = message;
|
|
99
|
-
banner.hidden = false;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const promptAlert = document.getElementById('prompt-alert');
|
|
103
|
-
const identityBar = document.getElementById('identity');
|
|
104
|
-
const identityPurpose = document.getElementById('identity-purpose');
|
|
105
|
-
const identityMeta = document.getElementById('identity-meta');
|
|
106
|
-
const sessionAgent = document.getElementById('session-agent');
|
|
107
|
-
|
|
108
|
-
// The document title doubles as the OS window title, the only thing a human
|
|
109
|
-
// sees when this viewer is one of several windows behind others. It has to
|
|
110
|
-
// carry both what the session IS and whether it wants something, so
|
|
111
|
-
// alt-tabbing is a decision rather than a hunt. Composed from state, never
|
|
112
|
-
// appended to, or repeated prompts would stack prefixes.
|
|
113
|
-
let identityTitle = `${sessionId} — ShellPuppet`;
|
|
114
|
-
let promptWaiting = false;
|
|
115
|
-
/** The daemon says the program is gone: the page stops pretending otherwise. */
|
|
116
|
-
let sessionEnded = false;
|
|
117
|
-
const refreshTitle = () => {
|
|
118
|
-
// Ended wins over waiting: a prompt on a dead session is the last frame
|
|
119
|
-
// it drew, not a question anyone can answer.
|
|
120
|
-
const prefix = sessionEnded ? '[ENDED] ' : promptWaiting ? '[ANSWER ME] ' : '';
|
|
121
|
-
document.title = prefix + identityTitle;
|
|
122
|
-
};
|
|
123
|
-
refreshTitle();
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* A session stays reachable for fifteen minutes after its process exits.
|
|
127
|
-
* Once the daemon says the program has gone, the page says so too, rather
|
|
128
|
-
* than showing the last frame the program drew as a live terminal.
|
|
129
|
-
*/
|
|
130
|
-
const showEnded = (exitCode) => {
|
|
131
|
-
if (sessionEnded) return;
|
|
132
|
-
sessionEnded = true;
|
|
133
|
-
readonlyButton.disabled = true;
|
|
134
|
-
readonlyButton.title = 'The program has exited';
|
|
135
|
-
modeHint.textContent =
|
|
136
|
-
exitCode === null || exitCode === undefined
|
|
137
|
-
? 'the program has exited; this session no longer accepts input'
|
|
138
|
-
: `the program has exited (code ${String(exitCode)}); this session no longer accepts input`;
|
|
139
|
-
refreshTitle();
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const showIdentity = (identity) => {
|
|
143
|
-
// Prefer the purpose: "fix the failing login test" tells you which window
|
|
144
|
-
// you want; "claude" does not, when three of them are claude.
|
|
145
|
-
const purpose = identity.purpose || identity.label || sessionId;
|
|
146
|
-
identityPurpose.textContent = purpose;
|
|
147
|
-
|
|
148
|
-
const meta = [identity.session, identity.command].filter(Boolean).join(' · ');
|
|
149
|
-
identityMeta.textContent = meta;
|
|
150
|
-
identityBar.hidden = false;
|
|
151
|
-
|
|
152
|
-
if (identity.startedBy) {
|
|
153
|
-
sessionAgent.textContent = `started by ${identity.startedBy}`;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (identity.readOnly === true) {
|
|
157
|
-
readOnlyEnforced = true;
|
|
158
|
-
readOnly = true;
|
|
159
|
-
showReadOnly();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Said on every connect, so a page that joins during the grace after the
|
|
163
|
-
// program exited knows straight away rather than on its first keystroke.
|
|
164
|
-
if (identity.exited === true) showEnded(identity.exitCode);
|
|
165
|
-
|
|
166
|
-
// Session id first: it is the short, unique, stable part, and window-title
|
|
167
|
-
// text is truncated from the RIGHT in the Windows taskbar and alt-tab.
|
|
168
|
-
identityTitle = `${identity.session} ${purpose} — ShellPuppet`;
|
|
169
|
-
refreshTitle();
|
|
170
|
-
relayout(false);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
const showPromptAlert = (question, waitingMs, options) => {
|
|
174
|
-
const seconds = Math.round((waitingMs || 0) / 1000);
|
|
175
|
-
// textContent throughout: the question and options are TUI output, i.e.
|
|
176
|
-
// untrusted, and innerHTML here would let a session paint markup into the
|
|
177
|
-
// supervisor's own chrome.
|
|
178
|
-
promptAlert.textContent = '';
|
|
179
|
-
const heading = document.createElement('div');
|
|
180
|
-
heading.textContent = `Waiting for you: ${String(seconds)}s unanswered`;
|
|
181
|
-
const question_ = document.createElement('div');
|
|
182
|
-
question_.className = 'question';
|
|
183
|
-
question_.textContent = question || '(the session is holding a prompt)';
|
|
184
|
-
promptAlert.append(heading, question_);
|
|
185
|
-
|
|
186
|
-
// The choices, so the badge says what is being asked and not merely that
|
|
187
|
-
// something is. Numbers come from the program's own rows. Never renumber
|
|
188
|
-
// them, because the number shown is the key the human has to press.
|
|
189
|
-
if (Array.isArray(options) && options.length > 0) {
|
|
190
|
-
const list = document.createElement('ul');
|
|
191
|
-
list.className = 'options';
|
|
192
|
-
for (const option of options) {
|
|
193
|
-
const item = document.createElement('li');
|
|
194
|
-
item.textContent = option;
|
|
195
|
-
list.append(item);
|
|
196
|
-
}
|
|
197
|
-
promptAlert.append(list);
|
|
198
|
-
}
|
|
199
|
-
promptAlert.hidden = false;
|
|
200
|
-
promptWaiting = true;
|
|
201
|
-
refreshTitle();
|
|
202
|
-
relayout(false);
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const clearPromptAlert = () => {
|
|
206
|
-
promptAlert.hidden = true;
|
|
207
|
-
promptAlert.textContent = '';
|
|
208
|
-
promptWaiting = false;
|
|
209
|
-
refreshTitle();
|
|
210
|
-
relayout(false);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
// Only the session id is required to get this far. Whether this page may
|
|
214
|
-
// actually watch is settled by the cookie on the socket, not by anything
|
|
215
|
-
// visible here, so a missing or spent grant surfaces as a failed connection
|
|
216
|
-
// rather than as a check in this file (§4.1).
|
|
217
|
-
if (!sessionId) {
|
|
218
|
-
showBanner(
|
|
219
|
-
'This viewer link is missing its session id. Ask the agent for a fresh viewer URL.',
|
|
220
|
-
);
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const term = new Terminal({
|
|
225
|
-
cursorBlink: true,
|
|
226
|
-
// Start at the human's saved size, not the default: the grid this page
|
|
227
|
-
// asks for is measured at whatever size the text already is, so building
|
|
228
|
-
// the terminal at the default would propose a grid for a size the human
|
|
229
|
-
// did not choose and then re-propose once it was applied.
|
|
230
|
-
fontSize: fontOverride ?? DEFAULT_FONT_PX,
|
|
231
|
-
scrollback: 5000,
|
|
232
|
-
allowProposedApi: true,
|
|
233
|
-
fontFamily: 'ui-monospace, "Cascadia Code", Consolas, monospace',
|
|
234
|
-
theme: { background: '#0b0e14', foreground: '#c5cdd9', cursor: '#6cb6ff' },
|
|
235
|
-
});
|
|
236
|
-
const fit = new FitAddon.FitAddon();
|
|
237
|
-
term.loadAddon(fit);
|
|
238
|
-
term.open(document.getElementById('term'));
|
|
239
|
-
|
|
240
|
-
let ws = null;
|
|
241
|
-
let readOnly = false;
|
|
242
|
-
/** Set by the daemon: this credential may watch but not type (§4.1). */
|
|
243
|
-
let readOnlyEnforced = false;
|
|
244
|
-
let retriesLeft = 8;
|
|
245
|
-
/** Distinguishes "the session ended" from "this link was already spent". */
|
|
246
|
-
let everConnected = false;
|
|
247
|
-
|
|
248
|
-
// ------------------------------------------------------------------------
|
|
249
|
-
// Repaints come from the daemon, never from this page. xterm reflows its
|
|
250
|
-
// buffer IN PLACE when the grid changes, rewrapping old lines into positions
|
|
251
|
-
// the program never drew and pushing rows off the top into the scrollback.
|
|
252
|
-
// So after any grid change the daemon sends a snapshot of its own renderer
|
|
253
|
-
// at the new size (a wipe through the write queue, then the rebuild), and on
|
|
254
|
-
// connect the same. A tail of the raw stream replayed here would start
|
|
255
|
-
// mid-frame, and a program that only repaints what changed never puts the
|
|
256
|
-
// rest of its frame back.
|
|
257
|
-
|
|
258
|
-
// Registered exactly once. See the header comment.
|
|
259
|
-
term.onData((data) => {
|
|
260
|
-
if (sessionEnded || readOnly) return;
|
|
261
|
-
if (ws && ws.readyState === 1) ws.send(data);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
function showReadOnly() {
|
|
265
|
-
readonlyButton.textContent = `read-only: ${readOnly ? 'on' : 'off'}`;
|
|
266
|
-
readonlyButton.classList.toggle('active', readOnly);
|
|
267
|
-
// Both write the same line, and "the program has exited" is the more
|
|
268
|
-
// useful of the two facts once it is true.
|
|
269
|
-
if (sessionEnded) return;
|
|
270
|
-
if (readOnlyEnforced) {
|
|
271
|
-
readonlyButton.disabled = true;
|
|
272
|
-
readonlyButton.title = 'This viewer link grants watching only';
|
|
273
|
-
modeHint.textContent = 'read-only link: the session will not accept your keystrokes';
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
modeHint.textContent = readOnly
|
|
277
|
-
? 'read-only: your keystrokes stay local'
|
|
278
|
-
: 'you and the agent share this session: type freely';
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
readonlyButton.addEventListener('click', () => {
|
|
282
|
-
// A watch-only credential is not a toggle. The daemon drops input from one
|
|
283
|
-
// regardless, so offering the switch would only promise something the
|
|
284
|
-
// session will not honour.
|
|
285
|
-
if (readOnlyEnforced) return;
|
|
286
|
-
readOnly = !readOnly;
|
|
287
|
-
showReadOnly();
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
// Who drives the grid lives on the DAEMON, not here: the daemon states it in
|
|
291
|
-
// a `geometry` frame on every connect, so a second copy here could only drift
|
|
292
|
-
// from the authority and would have to be kept in step for no benefit.
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Re-lay-out the terminal for the space it currently has. Called on window
|
|
296
|
-
* resize AND whenever a strip above the terminal appears or disappears. The
|
|
297
|
-
* identity bar and the prompt badge both consume vertical space, and without
|
|
298
|
-
* this the terminal keeps its old height and its top row is clipped behind
|
|
299
|
-
* them.
|
|
300
|
-
*/
|
|
301
|
-
function relayout(deliberate) {
|
|
302
|
-
// Nothing is sized until the daemon has said what the geometry IS. The
|
|
303
|
-
// identity frame arrives before the geometry frame and shows the identity
|
|
304
|
-
// bar, which calls this; with the grid's owner still unknown, fitting to
|
|
305
|
-
// the window then would resize a session that is not this page's to
|
|
306
|
-
// resize.
|
|
307
|
-
if (!geometryDecided) return;
|
|
308
|
-
// Held means held: scale the text so the fixed grid fills what it can.
|
|
309
|
-
// A strip appearing above the terminal is nobody's act, so an adopted
|
|
310
|
-
// grid is refitted for the space left rather than reshaped. Otherwise a
|
|
311
|
-
// window that changed shape is a deliberate act, and a deliberate act
|
|
312
|
-
// takes the grid, which is the only way back for someone looking at a
|
|
313
|
-
// grid somebody else chose.
|
|
314
|
-
if (controlMode === 'held' || captureMode || (controlMode === 'adopt' && !deliberate)) {
|
|
315
|
-
applyFontPreference(gridCols, gridRows);
|
|
316
|
-
} else {
|
|
317
|
-
requestFit();
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Ask the daemon for the grid this window would like. Deliberately a
|
|
323
|
-
* REQUEST: the grid is resized when the daemon's geometry frame comes back,
|
|
324
|
-
* never here. Sizing locally first would have the page briefly render a
|
|
325
|
-
* snapshot serialised at the old grid, and leave the two disagreeing
|
|
326
|
-
* whenever the daemon declined.
|
|
327
|
-
*/
|
|
328
|
-
function requestFit() {
|
|
329
|
-
// Text size first, grid second: the proposal is measured at the size the
|
|
330
|
-
// answer will be drawn at, so nothing has to be redrawn when it arrives.
|
|
331
|
-
const font = drivingFontPx();
|
|
332
|
-
if (term.options.fontSize !== font) {
|
|
333
|
-
term.options.fontSize = font;
|
|
334
|
-
term.resize(term.cols, term.rows);
|
|
335
|
-
}
|
|
336
|
-
updateZoomButtons();
|
|
337
|
-
const proposed = propose();
|
|
338
|
-
if (proposed === null) return;
|
|
339
|
-
// Never ask for more than the daemon will give. Font sizes are whole
|
|
340
|
-
// pixels, so the grid at the chosen size can still overshoot the bound by
|
|
341
|
-
// a column or two, and asking for those would be answered with the grid
|
|
342
|
-
// already held, which would ask again.
|
|
343
|
-
const cols = Math.min(proposed.cols, gridMax.cols);
|
|
344
|
-
const rows = Math.min(proposed.rows, gridMax.rows);
|
|
345
|
-
// Already what we would ask for. Saying so again would have the daemon
|
|
346
|
-
// answer with the same frame, which would ask again: a loop. Unless this
|
|
347
|
-
// page is merely adopting the grid, when asking IS the point: a
|
|
348
|
-
// deliberate act on a grid that happens to fit still takes it. The answer
|
|
349
|
-
// comes back as `yours`, and the next look through here finds nothing to
|
|
350
|
-
// ask.
|
|
351
|
-
if (cols === gridCols && rows === gridRows && controlMode !== 'adopt') return;
|
|
352
|
-
if (ws && ws.readyState === 1) {
|
|
353
|
-
ws.send(JSON.stringify({ type: 'resize', cols, rows }));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/** The grid this window would hold at the text size currently drawn. */
|
|
358
|
-
function propose() {
|
|
359
|
-
let proposed;
|
|
360
|
-
try {
|
|
361
|
-
proposed = fit.proposeDimensions();
|
|
362
|
-
} catch {
|
|
363
|
-
return null;
|
|
364
|
-
}
|
|
365
|
-
if (!proposed || !proposed.cols || !proposed.rows) return null;
|
|
366
|
-
return { cols: proposed.cols, rows: proposed.rows };
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
function fixedFontPx() {
|
|
370
|
-
return fontOverride === null ? DEFAULT_FONT_PX : fontOverride;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* The text size for a viewport that DRIVES the grid, which is the ordinary
|
|
375
|
-
* case. A real terminal keeps its text one size and lets the window decide
|
|
376
|
-
* how many rows and columns fit; scaling the text to the window would
|
|
377
|
-
* change what fits, which would change the grid, which would change what
|
|
378
|
-
* fits. The one exception is a window past the bound the daemon puts on a
|
|
379
|
-
* grid (§5): it will never be given the grid its text size asks for, so
|
|
380
|
-
* the text grows instead, until the most it can be given fills the window.
|
|
381
|
-
*/
|
|
382
|
-
function drivingFontPx() {
|
|
383
|
-
// The smallest size, from the human's chosen size upwards, at which the
|
|
384
|
-
// grid this window holds fits within the bound. MEASURED at each size,
|
|
385
|
-
// never estimated from another: cell sizes do not scale with the font
|
|
386
|
-
// linearly, and an estimate that flipped between two sizes would have the
|
|
387
|
-
// page proposing two grids in turn, for ever. Measuring depends only on
|
|
388
|
-
// the window, the chosen size and the bound, so the same window always
|
|
389
|
-
// gets the same answer.
|
|
390
|
-
const fixed = fixedFontPx();
|
|
391
|
-
const drawn = term.options.fontSize;
|
|
392
|
-
let font = fixed;
|
|
393
|
-
try {
|
|
394
|
-
for (; font < MAX_FONT_PX; font++) {
|
|
395
|
-
term.options.fontSize = font;
|
|
396
|
-
const proposed = propose();
|
|
397
|
-
if (proposed === null) break;
|
|
398
|
-
if (proposed.cols <= gridMax.cols && proposed.rows <= gridMax.rows) break;
|
|
399
|
-
}
|
|
400
|
-
} finally {
|
|
401
|
-
term.options.fontSize = drawn;
|
|
402
|
-
}
|
|
403
|
-
return font;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// The grid the daemon last reported, so a window resize or a font change can
|
|
407
|
-
// re-apply it without waiting for another frame, and this page's standing
|
|
408
|
-
// in it: 'yours' (this viewport drives it), 'adopt' (another does, or nobody
|
|
409
|
-
// does yet) or 'held' (the session holds it and no viewport may change it).
|
|
410
|
-
let gridCols = 0;
|
|
411
|
-
let gridRows = 0;
|
|
412
|
-
// The most the daemon will give a viewport (§5); every geometry frame
|
|
413
|
-
// carries it. Until one has, nothing bounds a proposal.
|
|
414
|
-
let gridMax = { cols: Infinity, rows: Infinity };
|
|
415
|
-
let controlMode = 'adopt';
|
|
416
|
-
// Set by the geometry frame on each connect; until then relayout() is a
|
|
417
|
-
// no-op, so nothing this page does can resize the PTY before it knows
|
|
418
|
-
// whether the size belongs to an agent.
|
|
419
|
-
let geometryDecided = false;
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* Size a grid this page does not drive to fill the window by changing the
|
|
423
|
-
* FONT, never the grid.
|
|
424
|
-
*
|
|
425
|
-
* A grid this viewport does not drive must stay the size it is, so the page
|
|
426
|
-
* cannot fill the window by resizing the grid, and scales the text instead.
|
|
427
|
-
* Left at its natural pixel size, a 120x34 terminal renders into roughly
|
|
428
|
-
* 960x580 px, stranded in the corner of a large window. A window whose shape
|
|
429
|
-
* differs from the grid's keeps bars on one axis, which is the faithful
|
|
430
|
-
* answer: the alternative is stretching the characters out of shape.
|
|
431
|
-
*
|
|
432
|
-
* xterm's cell size scales with fontSize, so measuring the rendered screen
|
|
433
|
-
* at a known size gives the ratio to the size that fits. One correction pass
|
|
434
|
-
* is enough in practice; the loop guards against font metrics that do not
|
|
435
|
-
* scale perfectly linearly.
|
|
436
|
-
*/
|
|
437
|
-
function fitGridByFont(cols, rows) {
|
|
438
|
-
const wrap = document.getElementById('wrap');
|
|
439
|
-
if (!wrap) return;
|
|
440
|
-
|
|
441
|
-
const styles = window.getComputedStyle(wrap);
|
|
442
|
-
const availableWidth = wrap.clientWidth
|
|
443
|
-
- parseFloat(styles.paddingLeft) - parseFloat(styles.paddingRight);
|
|
444
|
-
const availableHeight = wrap.clientHeight
|
|
445
|
-
- parseFloat(styles.paddingTop) - parseFloat(styles.paddingBottom);
|
|
446
|
-
if (availableWidth <= 0 || availableHeight <= 0) return;
|
|
447
|
-
|
|
448
|
-
for (let pass = 0; pass < 4; pass++) {
|
|
449
|
-
const screen = term.element && term.element.querySelector('.xterm-screen');
|
|
450
|
-
if (!screen) return;
|
|
451
|
-
const renderedWidth = screen.offsetWidth;
|
|
452
|
-
const renderedHeight = screen.offsetHeight;
|
|
453
|
-
if (renderedWidth <= 0 || renderedHeight <= 0) return;
|
|
454
|
-
|
|
455
|
-
const scale = Math.min(
|
|
456
|
-
availableWidth / renderedWidth,
|
|
457
|
-
availableHeight / renderedHeight,
|
|
458
|
-
);
|
|
459
|
-
// Within one per cent of a perfect fit is as good as it needs to be; stop
|
|
460
|
-
// rather than oscillate on rounding.
|
|
461
|
-
if (Math.abs(scale - 1) < 0.01) return;
|
|
462
|
-
|
|
463
|
-
// Floor, deliberately: rounding up can overshoot the box and clip the last
|
|
464
|
-
// column or row, which is worse than a little slack. The cost is that the
|
|
465
|
-
// fit can stop up to one font-step short (13 x 1.05 floors back to 13 and
|
|
466
|
-
// returns). That is accepted, because a font size is a coarse unit and
|
|
467
|
-
// the alternative is a terminal whose edge is cut off.
|
|
468
|
-
const current = term.options.fontSize || DEFAULT_FONT_PX;
|
|
469
|
-
const next = clampFont(Math.floor(current * scale));
|
|
470
|
-
if (next === current) return;
|
|
471
|
-
|
|
472
|
-
term.options.fontSize = next;
|
|
473
|
-
term.resize(cols, rows);
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
function applyFontPreference(cols, rows) {
|
|
478
|
-
if (fontOverride === null) {
|
|
479
|
-
fitGridByFont(cols, rows);
|
|
480
|
-
} else if (term.options.fontSize !== fontOverride) {
|
|
481
|
-
term.options.fontSize = fontOverride;
|
|
482
|
-
term.resize(cols, rows);
|
|
483
|
-
}
|
|
484
|
-
updateZoomButtons();
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
function applyGeometry(cols, rows, max) {
|
|
488
|
-
if (!Number.isInteger(cols) || !Number.isInteger(rows)) return;
|
|
489
|
-
gridCols = cols;
|
|
490
|
-
gridRows = rows;
|
|
491
|
-
if (max && Number.isInteger(max.cols) && Number.isInteger(max.rows)) {
|
|
492
|
-
gridMax = { cols: max.cols, rows: max.rows };
|
|
493
|
-
}
|
|
494
|
-
// Resize before the snapshot that follows this frame on the wire is
|
|
495
|
-
// parsed: it was serialised at this grid.
|
|
496
|
-
if (term.cols !== cols || term.rows !== rows) term.resize(cols, rows);
|
|
497
|
-
if (controlMode === 'yours' && !captureMode) {
|
|
498
|
-
// Drawn at the size the proposal was measured at (see requestFit), so
|
|
499
|
-
// there is nothing to fit here; the frame that answers a proposal is
|
|
500
|
-
// followed by another look at the window, in case it moved meanwhile.
|
|
501
|
-
updateZoomButtons();
|
|
502
|
-
} else {
|
|
503
|
-
// A grid this page does not drive fills what it can by scaling the
|
|
504
|
-
// text. Bars on one axis are the faithful answer to a window of a
|
|
505
|
-
// different shape (§5).
|
|
506
|
-
applyFontPreference(cols, rows);
|
|
507
|
-
}
|
|
508
|
-
sizeLabel.textContent =
|
|
509
|
-
`${cols}×${rows}` +
|
|
510
|
-
(controlMode === 'held' ? ' (fixed)' : controlMode === 'adopt' ? ' (following)' : '');
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
function updateZoomButtons() {
|
|
514
|
-
zoomAutoButton.classList.toggle('active', fontOverride === null);
|
|
515
|
-
zoomAutoButton.title = fontOverride === null
|
|
516
|
-
? 'Text size chosen automatically (click - or + to set it yourself)'
|
|
517
|
-
: 'Choose the text size automatically again';
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
function setFontOverride(value) {
|
|
521
|
-
fontOverride = value;
|
|
522
|
-
try {
|
|
523
|
-
window.localStorage.setItem(FONT_PREF_KEY, value === null ? 'auto' : String(value));
|
|
524
|
-
} catch {
|
|
525
|
-
// Preference simply will not persist; the session still works.
|
|
526
|
-
}
|
|
527
|
-
// A session that holds its grid cannot be given a different one, so the
|
|
528
|
-
// text size is all this can change: the fixed grid is drawn larger or
|
|
529
|
-
// smaller inside the window.
|
|
530
|
-
if (controlMode === 'held' || captureMode) {
|
|
531
|
-
applyFontPreference(gridCols, gridRows);
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
// Otherwise a human choosing a text size is asking what a real terminal
|
|
536
|
-
// gives them: smaller text means MORE rows and columns, not the same grid
|
|
537
|
-
// drawn small with space around it. Set the size, then ask for the grid
|
|
538
|
-
// that now fits the window. That is a deliberate act, so it takes the
|
|
539
|
-
// grid if this page was not already driving it.
|
|
540
|
-
requestFit();
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
function nudgeFont(delta) {
|
|
544
|
-
const base = term.options.fontSize || DEFAULT_FONT_PX;
|
|
545
|
-
setFontOverride(clampFont(base + delta));
|
|
39
|
+
// A blocked preference store leaves the automatic font size in place.
|
|
546
40
|
}
|
|
547
41
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
sourceLabel.className = `hint ${who}`;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
function connect() {
|
|
560
|
-
// No credential in this URL: the cookie the daemon set when it redeemed
|
|
561
|
-
// this page's grant is attached by the browser (§4.1).
|
|
562
|
-
ws = new WebSocket(`ws://${location.host}/sessions/${sessionId}/stream`);
|
|
563
|
-
ws.binaryType = 'arraybuffer';
|
|
564
|
-
|
|
565
|
-
ws.onopen = () => {
|
|
566
|
-
everConnected = true;
|
|
567
|
-
dot.classList.remove('off');
|
|
568
|
-
banner.hidden = true;
|
|
569
|
-
retriesLeft = 8;
|
|
570
|
-
// The daemon's snapshot begins with a wipe, so a RECONNECT does not
|
|
571
|
-
// paint on top of what is already here.
|
|
572
|
-
// Geometry is deliberately NOT decided here. The daemon sends a
|
|
573
|
-
// `geometry` frame on every connect stating the PTY size and whether it
|
|
574
|
-
// is held, and we act on that. Fitting here instead would resize the
|
|
575
|
-
// PTY out from under a held scripted run in the window between opening
|
|
576
|
-
// the socket and being told the hold exists, and so would any relayout
|
|
577
|
-
// before that frame, which is what the flag is for (see relayout()).
|
|
578
|
-
geometryDecided = false;
|
|
579
|
-
};
|
|
580
|
-
|
|
581
|
-
ws.onmessage = (event) => {
|
|
582
|
-
if (event.data instanceof ArrayBuffer) {
|
|
583
|
-
if (captureMode && !firstFrameSeen) {
|
|
584
|
-
firstFrameSeen = true;
|
|
585
|
-
// The first binary frame is the daemon's snapshot. Raise the flag
|
|
586
|
-
// once it has been parsed AND drawn: a parse callback, then a frame
|
|
587
|
-
// for the renderer to catch up.
|
|
588
|
-
term.write(new Uint8Array(event.data), () => {
|
|
589
|
-
window.requestAnimationFrame(() => {
|
|
590
|
-
window.requestAnimationFrame(() => {
|
|
591
|
-
window.__shellpuppetPainted = true;
|
|
592
|
-
});
|
|
593
|
-
});
|
|
594
|
-
});
|
|
595
|
-
return;
|
|
596
|
-
}
|
|
597
|
-
term.write(new Uint8Array(event.data));
|
|
598
|
-
return;
|
|
599
|
-
}
|
|
42
|
+
const pane = ShellPuppetPane.create({
|
|
43
|
+
container: document.getElementById('term'),
|
|
44
|
+
sessionId,
|
|
45
|
+
captureMode,
|
|
46
|
+
redactedCapture,
|
|
47
|
+
initialFontOverride: fontOverride,
|
|
48
|
+
onTitleChange: (title) => { document.title = title; },
|
|
49
|
+
onFontOverrideChange: (value) => {
|
|
600
50
|
try {
|
|
601
|
-
|
|
602
|
-
if (control && control.type === 'input-source') {
|
|
603
|
-
showInputSource(control.source);
|
|
604
|
-
} else if (control && control.type === 'geometry') {
|
|
605
|
-
geometryDecided = true;
|
|
606
|
-
// A capture never drives the grid whatever it is told: resizing the
|
|
607
|
-
// PTY under the program for the sake of a picture would change the
|
|
608
|
-
// very thing being photographed.
|
|
609
|
-
controlMode = captureMode ? 'adopt' : control.control || 'adopt';
|
|
610
|
-
// Follow the daemon first: it is authoritative for what the program
|
|
611
|
-
// draws, and a grid wider than the PTY leaves stale content beyond
|
|
612
|
-
// the last column that nothing will ever overwrite.
|
|
613
|
-
applyGeometry(control.cols, control.rows, control.max);
|
|
614
|
-
// Driving, so fill the window. Merely adopting is passive: this page
|
|
615
|
-
// shows what it was given until the human does something.
|
|
616
|
-
if (controlMode === 'yours' && !captureMode) requestFit();
|
|
617
|
-
} else if (control && control.type === 'identity') {
|
|
618
|
-
showIdentity(control);
|
|
619
|
-
} else if (control && control.type === 'exited') {
|
|
620
|
-
// The program has gone; every watcher is told the moment it happens.
|
|
621
|
-
showEnded(control.exitCode);
|
|
622
|
-
} else if (control && control.type === 'input-refused') {
|
|
623
|
-
// The daemon refusing a keystroke because the program has gone, for
|
|
624
|
-
// a page that missed the frame above.
|
|
625
|
-
if (control.reason === 'exited') showEnded(control.exitCode);
|
|
626
|
-
} else if (control && control.type === 'prompt-stall') {
|
|
627
|
-
showPromptAlert(control.question, control.waitingMs, control.options);
|
|
628
|
-
} else if (control && control.type === 'prompt-clear') {
|
|
629
|
-
clearPromptAlert();
|
|
630
|
-
}
|
|
51
|
+
window.localStorage.setItem(FONT_PREF_KEY, value === null ? 'auto' : String(value));
|
|
631
52
|
} catch {
|
|
632
|
-
//
|
|
53
|
+
// The selected size still applies for this page.
|
|
633
54
|
}
|
|
634
|
-
}
|
|
55
|
+
},
|
|
56
|
+
elements: {
|
|
57
|
+
dot: document.getElementById('dot'),
|
|
58
|
+
banner,
|
|
59
|
+
size: document.getElementById('size'),
|
|
60
|
+
inputSource: document.getElementById('input-source'),
|
|
61
|
+
readonly: document.getElementById('readonly'),
|
|
62
|
+
modeHint: document.getElementById('mode-hint'),
|
|
63
|
+
zoomAuto: document.getElementById('zoom-auto'),
|
|
64
|
+
identity: document.getElementById('identity'),
|
|
65
|
+
identityPurpose: document.getElementById('identity-purpose'),
|
|
66
|
+
identityMeta: document.getElementById('identity-meta'),
|
|
67
|
+
sessionAgent: document.getElementById('session-agent'),
|
|
68
|
+
promptAlert: document.getElementById('prompt-alert'),
|
|
69
|
+
},
|
|
70
|
+
});
|
|
635
71
|
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
everConnected
|
|
643
|
-
? 'Disconnected. The session has probably ended or this viewer link has been revoked. ' +
|
|
644
|
-
'Ask the agent for a fresh viewer URL if the session is still running.'
|
|
645
|
-
: 'Could not connect. A viewer link works once, and only for a few minutes. ' +
|
|
646
|
-
'Ask the agent for a fresh viewer URL.',
|
|
647
|
-
);
|
|
648
|
-
}
|
|
649
|
-
};
|
|
650
|
-
}
|
|
72
|
+
document.getElementById('readonly').addEventListener('click', () => {
|
|
73
|
+
if (!pane.isReadOnlyEnforced()) pane.setReadOnly(!pane.isReadOnly());
|
|
74
|
+
});
|
|
75
|
+
document.getElementById('zoom-in').addEventListener('click', () => pane.nudgeFont(1));
|
|
76
|
+
document.getElementById('zoom-out').addEventListener('click', () => pane.nudgeFont(-1));
|
|
77
|
+
document.getElementById('zoom-auto').addEventListener('click', () => pane.setFontOverride(null));
|
|
651
78
|
|
|
652
79
|
let resizeTimer = null;
|
|
653
80
|
window.addEventListener('resize', () => {
|
|
654
81
|
clearTimeout(resizeTimer);
|
|
655
|
-
resizeTimer = setTimeout(() => relayout(true), 120);
|
|
82
|
+
resizeTimer = setTimeout(() => pane.relayout(true), 120);
|
|
656
83
|
});
|
|
657
|
-
|
|
658
|
-
updateZoomButtons();
|
|
659
|
-
|
|
660
|
-
connect();
|
|
661
84
|
})();
|