townmouse 0.0.0-stage → 0.4.0
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/CHANGELOG.md +196 -0
- package/DESIGN.md +153 -0
- package/LICENSE +661 -0
- package/README.md +600 -2
- package/SECURITY.md +60 -0
- package/TOWNMOUSE.md +91 -0
- package/assets/brand/mascot/README.md +23 -0
- package/assets/brand/mascot/townmouse-aristocrat.png +0 -0
- package/assets/brand/mascot/townmouse-icon.svg +1 -0
- package/assets/brand/mascot/townmouse-monocled.png +0 -0
- package/bin/townmouse +68 -0
- package/docs/PACKAGING.md +371 -0
- package/docs/browser-workspace.md +237 -0
- package/docs/panes.md +442 -0
- package/native/TownmouseApp.swift +108 -0
- package/npm-shrinkwrap.json +1282 -0
- package/package.json +93 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/brand/floydslabs-credit.png +0 -0
- package/public/brand/floydslabs-credit.webp +0 -0
- package/public/brand/townmouse-aristocrat.webp +0 -0
- package/public/brand/townmouse-monocled.webp +0 -0
- package/public/browser-picture.mjs +159 -0
- package/public/clipboard-text.mjs +5 -0
- package/public/favicon-16x16.png +0 -0
- package/public/favicon-32x32.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/favicon.svg +1 -0
- package/public/features/app-version.mjs +35 -0
- package/public/features/autosave.mjs +80 -0
- package/public/features/clipboard.mjs +69 -0
- package/public/features/command-palette.mjs +181 -0
- package/public/features/desktop-palette.mjs +19 -0
- package/public/features/export.mjs +37 -0
- package/public/features/file-drop.mjs +77 -0
- package/public/features/floating-palette.mjs +113 -0
- package/public/features/fontsize.mjs +72 -0
- package/public/features/haptic-keys.mjs +42 -0
- package/public/features/history-undo.mjs +102 -0
- package/public/features/keepalive.mjs +36 -0
- package/public/features/keybar-config.mjs +70 -0
- package/public/features/keyboard-shortcuts.mjs +74 -0
- package/public/features/links.mjs +139 -0
- package/public/features/multi-window-sync.mjs +76 -0
- package/public/features/osc-bridge.mjs +64 -0
- package/public/features/phone-landscape-focus.mjs +157 -0
- package/public/features/recent-commands.mjs +100 -0
- package/public/features/search.mjs +134 -0
- package/public/features/session-lock.mjs +96 -0
- package/public/features/session-manager.mjs +251 -0
- package/public/features/settings-sync.mjs +77 -0
- package/public/features/shortcut-help.mjs +85 -0
- package/public/features/smart-completions.mjs +101 -0
- package/public/features/smart-suggest.mjs +124 -0
- package/public/features/snippets.mjs +85 -0
- package/public/features/split-view.mjs +102 -0
- package/public/features/status-metrics.mjs +137 -0
- package/public/features/stylus-support.mjs +124 -0
- package/public/features/swipe-history.mjs +141 -0
- package/public/features/swipe-sessions.mjs +85 -0
- package/public/features/tab-reorder.mjs +70 -0
- package/public/features/theme-system.mjs +51 -0
- package/public/features/toast-status.mjs +118 -0
- package/public/features/two-finger-menu.mjs +87 -0
- package/public/features/voice-input.mjs +244 -0
- package/public/features/wake-fullscreen.mjs +67 -0
- package/public/features/window-title.mjs +47 -0
- package/public/features/zoom.mjs +80 -0
- package/public/icon-128.png +0 -0
- package/public/icon-180.png +0 -0
- package/public/icon-192.png +0 -0
- package/public/icon-48.png +0 -0
- package/public/icon-512-maskable.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/icon.svg +1 -0
- package/public/index.html +2304 -0
- package/public/input-chunks.mjs +22 -0
- package/public/input-guard.mjs +218 -0
- package/public/manifest.json +56 -0
- package/public/panes.css +1433 -0
- package/public/panes.html +211 -0
- package/public/panes.mjs +3183 -0
- package/public/service-worker.js +67 -0
- package/public/workspace.css +1621 -0
- package/public/workspace.mjs +1715 -0
- package/scripts/install-service.sh +303 -0
- package/scripts/node-runtime.sh +138 -0
- package/scripts/panes +568 -0
- package/scripts/prepare-native.sh +35 -0
- package/scripts/retire-legacy-service.sh +76 -0
- package/scripts/run-hooks +69 -0
- package/scripts/townmouse.sh +152 -0
- package/scripts/uninstall-service.sh +65 -0
- package/src/agent-guide.js +329 -0
- package/src/attachments.js +128 -0
- package/src/browser-dom.js +548 -0
- package/src/browser-workspace.js +1733 -0
- package/src/chrome-resolve.js +80 -0
- package/src/owner-page.js +43 -0
- package/src/pty-permissions.js +99 -0
- package/src/regex-worker.js +13 -0
- package/src/replay-text.js +189 -0
- package/src/request-boundary.js +58 -0
- package/src/safe-regex.js +87 -0
- package/src/server.js +1582 -0
- package/src/shell-resolve.js +42 -0
- package/src/static-deps.js +61 -0
- package/src/workspace-panes.js +1431 -0
package/scripts/panes
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// Native pane control for the Townmouse workspace.
|
|
4
|
+
// Terminal panes are spawned with TOWNMOUSE_API / TOWNMOUSE_TOKEN / TOWNMOUSE_PANE_ID
|
|
5
|
+
// already in the environment, so an agent never copies a connection code.
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const os = require('os');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
|
|
10
|
+
const api = (process.env.TOWNMOUSE_API || 'http://127.0.0.1:11002/api/panes').replace(/\/$/, '');
|
|
11
|
+
// The shells in the owner's terminal row answer beside the pane API, with the
|
|
12
|
+
// same token.
|
|
13
|
+
const sessionApi = api.replace(/\/api\/panes$/, '') + '/api/sessions';
|
|
14
|
+
let token = process.env.TOWNMOUSE_TOKEN || '';
|
|
15
|
+
if (!token) {
|
|
16
|
+
try { token = fs.readFileSync(path.join(os.homedir(), '.townmouse', 'pane-token'), 'utf8').trim(); } catch (_) { /* reported below */ }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function call(method, suffix, body, base = api) {
|
|
20
|
+
if (!token) throw new Error('No workspace token. Run from a Townmouse terminal pane or set TOWNMOUSE_TOKEN.');
|
|
21
|
+
const res = await fetch(base + suffix, {
|
|
22
|
+
method,
|
|
23
|
+
headers: { 'Content-Type': 'application/json', 'X-Townmouse-Token': token },
|
|
24
|
+
// A hung server must not hang the agent. Long enough for a 120s bounded
|
|
25
|
+
// wait plus the round trip.
|
|
26
|
+
signal: AbortSignal.timeout(Number(process.env.TOWNMOUSE_CLI_TIMEOUT_MS) || 135000),
|
|
27
|
+
...(body ? { body: JSON.stringify(body) } : {})
|
|
28
|
+
});
|
|
29
|
+
const data = await res.json().catch(() => ({}));
|
|
30
|
+
if (!res.ok) {
|
|
31
|
+
// Keep the structured body: a refused action (stale target, missing
|
|
32
|
+
// element) explains itself better than the bare error string.
|
|
33
|
+
throw Object.assign(new Error(data.error || data.message || `HTTP ${res.status}`), { data, status: res.status });
|
|
34
|
+
}
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Options that never take a value. Without this list, `--replace "text"` would
|
|
39
|
+
// consume the text as the flag's value and clear the field instead of filling
|
|
40
|
+
// it.
|
|
41
|
+
const BOOLEAN_FLAGS = new Set(['replace', 'elements', 'activate', 'accept', 'dismiss', 'force', 'help', 'no-wait', 'cancel']);
|
|
42
|
+
|
|
43
|
+
function args(list) {
|
|
44
|
+
const out = { _: [] };
|
|
45
|
+
for (let i = 0; i < list.length; i++) {
|
|
46
|
+
const value = list[i];
|
|
47
|
+
if (value.startsWith('--')) {
|
|
48
|
+
const key = value.slice(2);
|
|
49
|
+
if (BOOLEAN_FLAGS.has(key)) { out[key] = true; continue; }
|
|
50
|
+
const next = list[i + 1];
|
|
51
|
+
if (next && !next.startsWith('--')) { out[key] = next; i++; }
|
|
52
|
+
else out[key] = true;
|
|
53
|
+
} else out._.push(value);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function print(value) { console.log(JSON.stringify(value, null, 2)); }
|
|
59
|
+
|
|
60
|
+
// How many leading tokens are `pane exec` options rather than the command.
|
|
61
|
+
function leadingOptionCount(list) {
|
|
62
|
+
let count = 0;
|
|
63
|
+
while (count < list.length) {
|
|
64
|
+
const value = list[count];
|
|
65
|
+
if (value === '--no-wait') { count += 1; continue; }
|
|
66
|
+
if (value === '--wait-ms' || value === '--timeout') { count += 2; continue; }
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
return count;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const MIME_BY_EXT = {
|
|
73
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
|
|
74
|
+
'.webp': 'image/webp', '.svg': 'image/svg+xml', '.pdf': 'application/pdf',
|
|
75
|
+
'.txt': 'text/plain', '.md': 'text/markdown', '.json': 'application/json',
|
|
76
|
+
'.zip': 'application/zip'
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function shellQuote(value) {
|
|
80
|
+
return `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Pane ids look like `p_1a2b3c4d5e6f`. Inside a pane the id can be omitted
|
|
84
|
+
// entirely, and then the remaining arguments are the whole command — so the id
|
|
85
|
+
// has to be recognised by shape, not by position.
|
|
86
|
+
function paneTarget(list, verb) {
|
|
87
|
+
if (list.length && /^p_[0-9a-f]+$/i.test(list[0])) {
|
|
88
|
+
return { paneId: list[0], rest: list.slice(1) };
|
|
89
|
+
}
|
|
90
|
+
const paneId = process.env.TOWNMOUSE_PANE_ID || '';
|
|
91
|
+
if (!paneId) throw new Error(`${verb} requires a pane id, or TOWNMOUSE_PANE_ID (run inside a Townmouse pane).`);
|
|
92
|
+
return { paneId, rest: list };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// A shell session is named by its id, by the first 8 or more characters of
|
|
96
|
+
// its id, or by its name when no other session has the same one. Names are
|
|
97
|
+
// looked up in the list the server shows this caller, which for an agent is
|
|
98
|
+
// the shells agents started: one of the owner's comes back as not found.
|
|
99
|
+
async function sessionTarget(reference, verb) {
|
|
100
|
+
const wanted = String(reference || '').trim();
|
|
101
|
+
if (!wanted) throw new Error(`${verb} requires a session: its name, its id, or the start of its id.`);
|
|
102
|
+
const state = await call('GET', '', undefined, sessionApi);
|
|
103
|
+
const sessions = Array.isArray(state.sessions) ? state.sessions : [];
|
|
104
|
+
const exact = sessions.find((session) => session.id === wanted || session.name === wanted);
|
|
105
|
+
if (exact) return exact;
|
|
106
|
+
const partial = /^[0-9a-f-]{8,}$/i.test(wanted) ? sessions.filter((session) => session.id.startsWith(wanted)) : [];
|
|
107
|
+
if (partial.length > 1) throw new Error(`“${wanted}” matches ${partial.length} sessions; use more of the id.`);
|
|
108
|
+
if (partial.length === 1) return partial[0];
|
|
109
|
+
throw new Error(`No session is named “${wanted}”. Run: panes session list`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function main() {
|
|
113
|
+
const [scope, action, ...rest] = process.argv.slice(2);
|
|
114
|
+
if (scope === 'pane' && (action === 'run' || action === 'input')) {
|
|
115
|
+
const target = paneTarget(rest, `pane ${action}`);
|
|
116
|
+
const payload = target.rest.join(' ');
|
|
117
|
+
if (action === 'run') {
|
|
118
|
+
print(await call('POST', `/panes/${target.paneId}/command`, { command: payload }));
|
|
119
|
+
} else {
|
|
120
|
+
print(await call('POST', `/panes/${target.paneId}/input`, { data: payload }));
|
|
121
|
+
}
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (scope === 'pane' && action === 'send-text') {
|
|
125
|
+
// paneTarget already removed an explicit id. Slicing the original list as
|
|
126
|
+
// though one was always present dropped the first word of the text when the
|
|
127
|
+
// pane came from the environment.
|
|
128
|
+
const target = paneTarget(rest, 'pane send-text');
|
|
129
|
+
const paneId = target.paneId;
|
|
130
|
+
let textParts = [...target.rest];
|
|
131
|
+
let bracketed = true;
|
|
132
|
+
if (textParts[0] === '--raw') { bracketed = false; textParts = textParts.slice(1); }
|
|
133
|
+
let text = textParts.join(' ');
|
|
134
|
+
if (bracketed) text = '\x1b[200~' + text + '\x1b[201~';
|
|
135
|
+
print(await call('POST', `/panes/${paneId}/input`, { data: text }));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (scope === 'pane' && action === 'keys') {
|
|
139
|
+
const { paneId, rest: keys } = paneTarget(rest, 'pane keys');
|
|
140
|
+
if (!keys.length) throw new Error('pane keys requires at least one key name.');
|
|
141
|
+
print(await call('POST', `/panes/${paneId}/keys`, { keys }));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (scope === 'pane' && action === 'status') {
|
|
145
|
+
const { paneId, rest: remaining } = paneTarget(rest, 'pane status');
|
|
146
|
+
const options = args(remaining);
|
|
147
|
+
const state = options.state || options._[0];
|
|
148
|
+
if (!state) throw new Error('pane status requires --state working|blocked|idle|done|unknown.');
|
|
149
|
+
print(await call('POST', `/panes/${paneId}/status`, { state, message: options.message }));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (scope === 'pane' && action === 'dialog') {
|
|
153
|
+
const { paneId, rest: remaining } = paneTarget(rest, 'pane dialog');
|
|
154
|
+
const options = args(remaining);
|
|
155
|
+
const dialogAction = options.accept ? 'accept' : options.dismiss ? 'dismiss' : '';
|
|
156
|
+
if (!dialogAction) throw new Error('pane dialog requires --accept or --dismiss.');
|
|
157
|
+
print(await call('POST', `/panes/${paneId}/browser/dialog`, {
|
|
158
|
+
action: dialogAction,
|
|
159
|
+
text: options.text
|
|
160
|
+
}));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (scope === 'pane' && action === 'wait') {
|
|
164
|
+
const { paneId, rest: remaining } = paneTarget(rest, 'pane wait');
|
|
165
|
+
const options = args(remaining);
|
|
166
|
+
const query = new URLSearchParams();
|
|
167
|
+
if (options.match !== undefined) query.set('match', String(options.match));
|
|
168
|
+
if (options.regex !== undefined) query.set('regex', String(options.regex));
|
|
169
|
+
if (options.idle !== undefined) query.set('idleMs', String(options.idle));
|
|
170
|
+
if (options.state !== undefined) query.set('state', String(options.state));
|
|
171
|
+
if (options.timeout !== undefined) query.set('timeout', String(options.timeout));
|
|
172
|
+
if (options.since !== undefined) query.set('since', String(options.since));
|
|
173
|
+
if (!query.toString()) throw new Error('pane wait requires --match, --regex, --idle, or --state.');
|
|
174
|
+
print(await call('GET', `/panes/${paneId}/wait?${query.toString()}`));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (scope === 'pane' && action === 'request') {
|
|
178
|
+
// The one way an agent gets at one of the owner's panes: ask, and wait for
|
|
179
|
+
// the answer. The answer is the pane's own state, so this reads the same
|
|
180
|
+
// truth the owner's page shows.
|
|
181
|
+
const target = paneTarget(rest, 'pane request');
|
|
182
|
+
const paneId = target.paneId;
|
|
183
|
+
const options = args(target.rest);
|
|
184
|
+
if (options.cancel) {
|
|
185
|
+
print(await call('DELETE', `/panes/${paneId}/request-access`));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const level = options.level || 'control';
|
|
189
|
+
if (!['view', 'control'].includes(level)) throw new Error('pane request requires --level view or control.');
|
|
190
|
+
await call('POST', `/panes/${paneId}/request-access`, { level, message: options.message });
|
|
191
|
+
const rank = { private: 0, view: 1, control: 2 };
|
|
192
|
+
const readPane = async () => {
|
|
193
|
+
const state = await call('GET', '/state');
|
|
194
|
+
for (const tab of state.tabs || []) {
|
|
195
|
+
const pane = (tab.panes || []).find((candidate) => candidate.id === paneId);
|
|
196
|
+
if (pane) return pane;
|
|
197
|
+
}
|
|
198
|
+
return null;
|
|
199
|
+
};
|
|
200
|
+
const deadline = Date.now() + Math.max(0, Number(options.timeout) || 120000);
|
|
201
|
+
let answered = 'waiting';
|
|
202
|
+
let pane = await readPane();
|
|
203
|
+
for (;;) {
|
|
204
|
+
const has = pane && pane.agentAccess ? rank[pane.agentAccess.level] || 0 : 0;
|
|
205
|
+
if (pane && has >= rank[level]) { answered = 'granted'; break; }
|
|
206
|
+
if (!pane || !pane.accessRequest) { answered = 'declined'; break; }
|
|
207
|
+
if (options['no-wait'] || Date.now() >= deadline) break;
|
|
208
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
209
|
+
pane = await readPane();
|
|
210
|
+
}
|
|
211
|
+
print({
|
|
212
|
+
paneId,
|
|
213
|
+
requested: level,
|
|
214
|
+
answered,
|
|
215
|
+
agentAccess: pane ? pane.agentAccess : null,
|
|
216
|
+
// While the ask waits, this says until when.
|
|
217
|
+
accessRequest: pane && pane.accessRequest ? pane.accessRequest : null,
|
|
218
|
+
note: answered === 'granted' ? 'The owner gave access. It lasts until the owner changes it or the server restarts.'
|
|
219
|
+
: answered === 'declined' ? 'The owner dismissed the ask, or it expired unanswered. Finish without this pane; do not ask again at once.'
|
|
220
|
+
: 'The owner has not answered yet. Run the command again to keep waiting.'
|
|
221
|
+
});
|
|
222
|
+
if (answered === 'waiting') process.exitCode = 3;
|
|
223
|
+
else if (answered === 'declined') process.exitCode = 5;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (scope === 'guide') {
|
|
227
|
+
print(await call('GET', '/guide'));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (scope === 'pane' && action === 'exec') {
|
|
231
|
+
const { paneId, rest: parts } = paneTarget(rest, 'pane exec');
|
|
232
|
+
let wait = true;
|
|
233
|
+
let timeoutMs;
|
|
234
|
+
// Everything after the first `--` is the command, verbatim. Without a
|
|
235
|
+
// separator, only leading options are consumed; the rest is the command.
|
|
236
|
+
const separator = parts.indexOf('--');
|
|
237
|
+
const optionPart = separator >= 0 ? parts.slice(0, separator) : parts.slice(0, leadingOptionCount(parts));
|
|
238
|
+
const commandPart = separator >= 0 ? parts.slice(separator + 1) : parts.slice(optionPart.length);
|
|
239
|
+
for (let i = 0; i < optionPart.length; i++) {
|
|
240
|
+
const value = optionPart[i];
|
|
241
|
+
if (value === '--no-wait') { wait = false; continue; }
|
|
242
|
+
if (value === '--wait-ms' || value === '--timeout') { timeoutMs = Number(optionPart[++i]); continue; }
|
|
243
|
+
throw new Error(`Unknown pane exec option: ${value}. Put the command after --.`);
|
|
244
|
+
}
|
|
245
|
+
const command = commandPart.join(' ');
|
|
246
|
+
if (!command) throw new Error('pane exec requires a command.');
|
|
247
|
+
const result = await call('POST', `/panes/${paneId}/jobs`, { command, wait, timeoutMs });
|
|
248
|
+
print(result);
|
|
249
|
+
if (result.status === 'running') process.exitCode = 3;
|
|
250
|
+
else if (result.status !== 'completed') process.exitCode = 1;
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (scope === 'pane' && action === 'job') {
|
|
254
|
+
// `panes pane job <job-id>` works inside a pane; otherwise supply both.
|
|
255
|
+
const hasPaneId = rest.length > 1 && /^p_[0-9a-f]+$/i.test(rest[0]);
|
|
256
|
+
const paneId = hasPaneId ? rest[0] : (process.env.TOWNMOUSE_PANE_ID || '');
|
|
257
|
+
const jobId = hasPaneId ? rest[1] : rest[0];
|
|
258
|
+
if (!paneId || !jobId) throw new Error('pane job requires a job id, or a pane id and a job id.');
|
|
259
|
+
const result = await call('GET', `/panes/${paneId}/jobs/${jobId}`);
|
|
260
|
+
print(result);
|
|
261
|
+
if (result.status === 'running') process.exitCode = 3;
|
|
262
|
+
else if (result.status !== 'completed') process.exitCode = 1;
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (scope === 'pane' && action === 'browser') {
|
|
266
|
+
// Accept the documented `browser <verb> <pane-id> ...`, the id-first
|
|
267
|
+
// `browser <pane-id> <verb> ...`, and `browser <verb> ...` inside a pane.
|
|
268
|
+
const parts = [...rest];
|
|
269
|
+
let paneId = '';
|
|
270
|
+
if (parts.length > 1 && /^p_[0-9a-f]+$/i.test(parts[0])) paneId = parts.shift();
|
|
271
|
+
const verb = parts.shift();
|
|
272
|
+
if (!paneId && parts.length && /^p_[0-9a-f]+$/i.test(parts[0])) paneId = parts.shift();
|
|
273
|
+
if (!paneId) paneId = process.env.TOWNMOUSE_PANE_ID || '';
|
|
274
|
+
const options = args(parts);
|
|
275
|
+
if (!paneId || !verb) throw new Error('pane browser requires an action (run inside a Townmouse pane, or pass a pane id).');
|
|
276
|
+
const base = `/panes/${paneId}/browser`;
|
|
277
|
+
const number = (value) => (value === undefined ? undefined : Number(value));
|
|
278
|
+
const finish = (value) => {
|
|
279
|
+
print(value);
|
|
280
|
+
if (value && value.ok === false) process.exitCode = value.status === 'stale' ? 4 : 1;
|
|
281
|
+
return value;
|
|
282
|
+
};
|
|
283
|
+
switch (verb) {
|
|
284
|
+
case 'observe': {
|
|
285
|
+
const query = [];
|
|
286
|
+
if (options.elements) query.push('elements=1');
|
|
287
|
+
if (options.max) query.push(`max=${encodeURIComponent(options.max)}`);
|
|
288
|
+
const result = await call('GET', `${base}/observe${query.length ? '?' + query.join('&') : ''}`);
|
|
289
|
+
if (options['image-file'] && result.image) {
|
|
290
|
+
fs.writeFileSync(String(options['image-file']), Buffer.from(result.image, 'base64'));
|
|
291
|
+
result.imageFile = String(options['image-file']);
|
|
292
|
+
delete result.image;
|
|
293
|
+
}
|
|
294
|
+
print(result);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
case 'elements': {
|
|
298
|
+
const query = options.max ? `?max=${encodeURIComponent(options.max)}` : '';
|
|
299
|
+
print(await call('GET', `${base}/elements${query}`));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
case 'click':
|
|
303
|
+
case 'hover': {
|
|
304
|
+
const body = { action: verb };
|
|
305
|
+
if (options.ref) body.ref = String(options.ref);
|
|
306
|
+
else { body.x = number(options.x); body.y = number(options.y); }
|
|
307
|
+
if (options.observation !== undefined) body.observationId = Number(options.observation);
|
|
308
|
+
if (options.force) body.force = true;
|
|
309
|
+
finish(await call('POST', `${base}/action`, body));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
case 'type': {
|
|
313
|
+
const body = { action: 'type', text: options._.join(' ') };
|
|
314
|
+
if (options.ref) body.ref = String(options.ref);
|
|
315
|
+
if (options.replace) body.replace = true;
|
|
316
|
+
finish(await call('POST', `${base}/action`, body));
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
case 'key': {
|
|
320
|
+
const keys = options._.slice(0).map(String);
|
|
321
|
+
if (!keys.length) throw new Error('pane browser key requires at least one key.');
|
|
322
|
+
finish(await call('POST', `${base}/action`, { action: 'key', keys }));
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
case 'drag': {
|
|
326
|
+
finish(await call('POST', `${base}/action`, {
|
|
327
|
+
action: 'drag',
|
|
328
|
+
fromRef: options['from-ref'],
|
|
329
|
+
toRef: options['to-ref'],
|
|
330
|
+
fromX: number(options['from-x']),
|
|
331
|
+
fromY: number(options['from-y']),
|
|
332
|
+
toX: number(options['to-x']),
|
|
333
|
+
toY: number(options['to-y'])
|
|
334
|
+
}));
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
case 'scroll': {
|
|
338
|
+
finish(await call('POST', `${base}/action`, {
|
|
339
|
+
action: 'scroll',
|
|
340
|
+
ref: options.ref,
|
|
341
|
+
selector: options.selector,
|
|
342
|
+
dx: number(options.dx),
|
|
343
|
+
dy: number(options.dy ?? options.amount)
|
|
344
|
+
}));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
case 'select': {
|
|
348
|
+
if (!options.ref) throw new Error('pane browser select requires --ref.');
|
|
349
|
+
finish(await call('POST', `${base}/action`, { action: 'select', ref: String(options.ref), value: options.value }));
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
case 'navigate': {
|
|
353
|
+
const url = options._[0];
|
|
354
|
+
if (!url) throw new Error('pane browser navigate requires a URL.');
|
|
355
|
+
print(await call('POST', `${base}/action`, { action: 'navigate', url }));
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
case 'back':
|
|
359
|
+
case 'forward':
|
|
360
|
+
case 'reload':
|
|
361
|
+
print(await call('POST', `${base}/action`, { action: verb }));
|
|
362
|
+
return;
|
|
363
|
+
case 'wait': {
|
|
364
|
+
print(await call('POST', `${base}/wait`, {
|
|
365
|
+
url: options.url,
|
|
366
|
+
title: options.title,
|
|
367
|
+
text: options.text,
|
|
368
|
+
selector: options.selector,
|
|
369
|
+
value: options.value,
|
|
370
|
+
ref: options.ref,
|
|
371
|
+
timeout: number(options.timeout)
|
|
372
|
+
}));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
case 'text': {
|
|
376
|
+
print(await call('GET', `${base}/content`));
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
case 'sizing': {
|
|
380
|
+
const mode = options._[0];
|
|
381
|
+
if (!['fit', 'fixed'].includes(mode)) throw new Error('pane browser sizing requires fit or fixed.');
|
|
382
|
+
print(await call('POST', `${base}/sizing`, { mode, width: number(options.width), height: number(options.height) }));
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
default:
|
|
386
|
+
throw new Error(`Unknown browser action: ${verb}`);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
// Shell sessions: the shells in the owner's terminal row. A session is named
|
|
390
|
+
// by its name, its id, or the start of its id.
|
|
391
|
+
if (scope === 'session') {
|
|
392
|
+
const options = args(rest);
|
|
393
|
+
if (action === 'list') {
|
|
394
|
+
print(await call('GET', '', undefined, sessionApi));
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (action === 'create') {
|
|
398
|
+
// createdBy is what older servers read to learn who was asking. A current
|
|
399
|
+
// server ignores it and decides from the key: the token makes this an
|
|
400
|
+
// agent's shell either way.
|
|
401
|
+
print(await call('POST', '', {
|
|
402
|
+
name: options.name,
|
|
403
|
+
cwd: options.cwd,
|
|
404
|
+
activate: options.activate === true,
|
|
405
|
+
createdBy: 'agent'
|
|
406
|
+
}, sessionApi));
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (action === 'rename') {
|
|
410
|
+
const target = await sessionTarget(options._[0], 'session rename');
|
|
411
|
+
if (!options.name) throw new Error('session rename requires --name TEXT.');
|
|
412
|
+
print(await call('PATCH', `/${target.id}`, { name: options.name }, sessionApi));
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (action === 'activate') {
|
|
416
|
+
const target = await sessionTarget(options._[0], 'session activate');
|
|
417
|
+
print(await call('POST', `/${target.id}/activate`, {}, sessionApi));
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (action === 'close') {
|
|
421
|
+
const target = await sessionTarget(options._[0], 'session close');
|
|
422
|
+
print(await call('DELETE', `/${target.id}`, undefined, sessionApi));
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
throw new Error(`Unknown session action: ${action}. Run: panes help`);
|
|
426
|
+
}
|
|
427
|
+
const a = args(rest);
|
|
428
|
+
if (scope === 'attach') {
|
|
429
|
+
const attachArgs = args([action, ...rest].filter((value) => value !== undefined));
|
|
430
|
+
const file = attachArgs._[0];
|
|
431
|
+
if (!file) throw new Error('attach requires a file path.');
|
|
432
|
+
if (!token) throw new Error('No workspace token. Run from a Townmouse terminal pane or set TOWNMOUSE_TOKEN.');
|
|
433
|
+
const stat = fs.statSync(file);
|
|
434
|
+
if (stat.size > 25 * 1024 * 1024) throw new Error('Attachment exceeds the 25 MB limit.');
|
|
435
|
+
const root = api.replace(/\/api\/panes\/?$/, '');
|
|
436
|
+
const type = attachArgs.type || MIME_BY_EXT[path.extname(file).toLowerCase()] || 'application/octet-stream';
|
|
437
|
+
const response = await fetch(root + '/api/attachments', {
|
|
438
|
+
method: 'POST',
|
|
439
|
+
headers: {
|
|
440
|
+
'Content-Type': type,
|
|
441
|
+
'X-Filename': encodeURIComponent(path.basename(file)),
|
|
442
|
+
'X-Townmouse-Token': token
|
|
443
|
+
},
|
|
444
|
+
body: fs.readFileSync(file)
|
|
445
|
+
});
|
|
446
|
+
const attachment = await response.json().catch(() => ({}));
|
|
447
|
+
if (!response.ok) throw new Error(attachment.error || `Upload failed (${response.status}).`);
|
|
448
|
+
if (attachArgs.pane) {
|
|
449
|
+
await call('POST', `/panes/${attachArgs.pane}/input`, { data: shellQuote(attachment.path) + ' ' });
|
|
450
|
+
attachment.insertedPane = attachArgs.pane;
|
|
451
|
+
}
|
|
452
|
+
print(attachment);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
if (!scope || scope === 'help' || scope === '--help') {
|
|
456
|
+
console.log(`Usage:
|
|
457
|
+
panes guide Print the agent instruction sheet.
|
|
458
|
+
panes state
|
|
459
|
+
panes tab list
|
|
460
|
+
panes tab create [--title TEXT] [--activate]
|
|
461
|
+
panes tab close <tab-id>
|
|
462
|
+
panes pane create --tab <tab-id> --kind terminal|browser [--url URL] [--cwd PATH] [--title TEXT]
|
|
463
|
+
panes pane url <pane-id> <url>
|
|
464
|
+
panes pane exec <pane-id> -- <command...> [--wait-ms N] [--no-wait]
|
|
465
|
+
Tracked job: exit code + output.
|
|
466
|
+
panes pane job <pane-id> <job-id> Re-read a tracked job.
|
|
467
|
+
panes pane run <pane-id> <command...> Type into the interactive terminal (unconfirmed).
|
|
468
|
+
panes pane read <pane-id> [--lines N] [--raw]
|
|
469
|
+
panes pane text <pane-id> Browser page text.
|
|
470
|
+
panes pane input <pane-id> <text...>
|
|
471
|
+
panes pane send-text <pane-id> [--raw] <text...>
|
|
472
|
+
panes pane keys <pane-id> <key...> (enter, esc, tab, ctrl+c, alt+x, arrows)
|
|
473
|
+
panes pane wait <pane-id> [--match TEXT | --regex PATTERN | --idle MS | --state STATE]
|
|
474
|
+
[--since OFFSET] [--timeout MS]
|
|
475
|
+
panes pane status <pane-id> --state working|blocked|idle|done|unknown [--message TEXT]
|
|
476
|
+
panes pane dialog <pane-id> --accept|--dismiss [--text VALUE]
|
|
477
|
+
panes pane request <pane-id> [--level view|control] [--message TEXT] [--timeout MS] [--no-wait]
|
|
478
|
+
Ask the owner for access to one of their panes, and wait
|
|
479
|
+
for the answer. --cancel withdraws the ask.
|
|
480
|
+
|
|
481
|
+
Browser control (pane ids may be omitted inside a Townmouse pane):
|
|
482
|
+
panes pane browser observe <pane-id> [--elements] [--max N] [--image-file PATH]
|
|
483
|
+
panes pane browser elements <pane-id> [--max N]
|
|
484
|
+
panes pane browser click <pane-id> [--ref e12 | --x N --y N [--observation N]]
|
|
485
|
+
panes pane browser hover <pane-id> [--ref e12 | --x N --y N]
|
|
486
|
+
panes pane browser drag <pane-id> --from-ref e12 --to-ref e15
|
|
487
|
+
panes pane browser type <pane-id> <text...> [--ref e12] [--replace]
|
|
488
|
+
panes pane browser key <pane-id> <key...> (Alt+M, Control+Shift+Tab, arrows)
|
|
489
|
+
panes pane browser scroll <pane-id> [--ref e12 | --selector CSS] [--dy N] [--dx N]
|
|
490
|
+
panes pane browser select <pane-id> --ref e12 --value V
|
|
491
|
+
panes pane browser navigate <pane-id> <url>
|
|
492
|
+
panes pane browser back|forward|reload <pane-id>
|
|
493
|
+
panes pane browser wait <pane-id> [--url U | --text T | --selector S | --value V --ref e12]
|
|
494
|
+
[--timeout MS]
|
|
495
|
+
panes pane browser sizing <pane-id> fit|fixed [--width N] [--height N]
|
|
496
|
+
panes pane close <pane-id>
|
|
497
|
+
panes attach <file> [--pane <pane-id>] [--type MIME]
|
|
498
|
+
panes active --tab <tab-id> [--pane <pane-id>]
|
|
499
|
+
|
|
500
|
+
Shell sessions (shells in the owner's terminal row). These commands reach the shells agents
|
|
501
|
+
started, which 'session list' shows; the owner's shells are only counted there, as ownerStarted.
|
|
502
|
+
<session> is a name, an id, or the start of an id from that list.
|
|
503
|
+
panes session list
|
|
504
|
+
panes session create [--name TEXT] [--cwd PATH] [--activate]
|
|
505
|
+
panes session rename <session> --name TEXT
|
|
506
|
+
panes session activate <session> Moves the owner's view; only when they asked.
|
|
507
|
+
panes session close <session> Ends the shell. It cannot be undone.
|
|
508
|
+
|
|
509
|
+
Exit codes: 0 done, 1 failed/missing, 3 still running or still waiting, 4 stale target,
|
|
510
|
+
5 the owner declined the request.
|
|
511
|
+
Who may do what: a pane you open is every agent's to use, and so is the Orchestrator pane.
|
|
512
|
+
A pane the owner opened is Private until the owner gives View only (you can read it) or
|
|
513
|
+
Allow control (you can read and drive it). 'panes state' says what you have on each pane
|
|
514
|
+
under agentAccess. A password field takes your typing and never returns its value.
|
|
515
|
+
A shell you start is any agent's to rename, show and close; a shell the owner started is the
|
|
516
|
+
owner's, so you cannot rename, show or close it, and 'session list' only counts it.
|
|
517
|
+
Environment: TOWNMOUSE_API, TOWNMOUSE_TOKEN, TOWNMOUSE_TAB_ID, TOWNMOUSE_PANE_ID.`);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (scope === 'state') { print(await call('GET', '/state')); return; }
|
|
521
|
+
if (scope === 'active') {
|
|
522
|
+
const activeArgs = args([action, ...rest].filter((value) => value !== undefined));
|
|
523
|
+
print(await call('POST', '/active', { tabId: activeArgs.tab, paneId: activeArgs.pane }));
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (scope === 'tab') {
|
|
527
|
+
if (action === 'list') { const s = await call('GET', '/state'); print(s.tabs); return; }
|
|
528
|
+
// createdBy is what older servers read to learn who was asking. A current
|
|
529
|
+
// server ignores it and decides from the key: the token makes this the
|
|
530
|
+
// agent's tab either way.
|
|
531
|
+
if (action === 'create') { print(await call('POST', '/tabs', { title: a.title, activate: a.activate === true, createdBy: 'agent' })); return; }
|
|
532
|
+
if (action === 'close') { print(await call('DELETE', `/tabs/${a._[0]}`)); return; }
|
|
533
|
+
}
|
|
534
|
+
if (scope === 'pane') {
|
|
535
|
+
const hasPaneId = a._.length > 0 && /^p_[0-9a-f]+$/i.test(a._[0]);
|
|
536
|
+
const paneId = hasPaneId ? a._[0] : (process.env.TOWNMOUSE_PANE_ID || '');
|
|
537
|
+
const positional = hasPaneId ? a._.slice(1) : a._;
|
|
538
|
+
if (action === 'create') {
|
|
539
|
+
const tab = a.tab || process.env.TOWNMOUSE_TAB_ID;
|
|
540
|
+
if (!tab) throw new Error('--tab is required (or run inside a Townmouse pane).');
|
|
541
|
+
print(await call('POST', `/tabs/${tab}/panes`, { kind: a.kind || 'terminal', url: a.url, cwd: a.cwd, title: a.title, createdBy: 'agent' }));
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (action === 'url') { print(await call('POST', `/panes/${paneId}/url`, { url: positional[0] })); return; }
|
|
545
|
+
if (action === 'run') { print(await call('POST', `/panes/${paneId}/command`, { command: positional.join(' ') })); return; }
|
|
546
|
+
if (action === 'input') { print(await call('POST', `/panes/${paneId}/input`, { data: positional.join(' ') })); return; }
|
|
547
|
+
if (action === 'text') { print(await call('GET', `/panes/${paneId}/browser/content`)); return; }
|
|
548
|
+
if (action === 'read') {
|
|
549
|
+
if (!paneId) throw new Error('pane read requires a pane id (or run inside a Townmouse pane).');
|
|
550
|
+
// Clean screen text by default; --raw keeps the original escape data.
|
|
551
|
+
const mode = a.raw === true ? 'raw' : 'clean';
|
|
552
|
+
const data = await call('GET', `/panes/${paneId}/text?mode=${mode}`);
|
|
553
|
+
const lines = Number(a.lines || 80);
|
|
554
|
+
const text = String(data.text || '').split('\n').slice(-lines).join('\n');
|
|
555
|
+
print({ ...data, text, data: text });
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (action === 'close') {
|
|
559
|
+
if (!paneId) throw new Error('pane close requires a pane id (or run inside a Townmouse pane).');
|
|
560
|
+
print(await call('DELETE', `/panes/${paneId}`));
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
console.error('Unknown command. Run: panes help');
|
|
565
|
+
process.exitCode = 2;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
main().catch((error) => { console.error(error.message); process.exitCode = 1; });
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Mark node-pty's macOS spawn helpers executable wherever npm placed them.
|
|
3
|
+
#
|
|
4
|
+
# npm hoists dependencies for a normal install (`npm install townmouse`, `npx`)
|
|
5
|
+
# and nests them for a global install, so the old package-local
|
|
6
|
+
# `$ROOT/node_modules/node-pty` path silently matched nothing and every terminal
|
|
7
|
+
# pane failed with "posix_spawnp failed". Resolve the real directory through
|
|
8
|
+
# Node instead, then fix the helpers there.
|
|
9
|
+
set -eu
|
|
10
|
+
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
|
|
11
|
+
NODE="${TERMINALONE_NODE:-$(command -v node 2>/dev/null || true)}"
|
|
12
|
+
|
|
13
|
+
node_pty_dir() {
|
|
14
|
+
[ -n "$NODE" ] || return 1
|
|
15
|
+
(
|
|
16
|
+
cd "$ROOT" || exit 1
|
|
17
|
+
"$NODE" -e '
|
|
18
|
+
const path = require("path");
|
|
19
|
+
const fs = require("fs");
|
|
20
|
+
let dir = path.dirname(require.resolve("node-pty"));
|
|
21
|
+
while (dir !== path.dirname(dir) && !fs.existsSync(path.join(dir, "package.json"))) {
|
|
22
|
+
dir = path.dirname(dir);
|
|
23
|
+
}
|
|
24
|
+
process.stdout.write(dir);
|
|
25
|
+
' 2>/dev/null
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
PTY_DIR=$(node_pty_dir || true)
|
|
30
|
+
[ -n "$PTY_DIR" ] || PTY_DIR="$ROOT/node_modules/node-pty"
|
|
31
|
+
[ -d "$PTY_DIR" ] || exit 0
|
|
32
|
+
|
|
33
|
+
for helper in "$PTY_DIR"/prebuilds/darwin-*/spawn-helper; do
|
|
34
|
+
[ ! -f "$helper" ] || chmod +x "$helper"
|
|
35
|
+
done
|