aegiscode 6.8.24 → 6.8.27
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/package.json +1 -1
- package/src/app.js +32 -1
- package/src/chatflow.js +135 -8
- package/src/clipboard.js +349 -66
- package/src/events.js +50 -0
- package/src/history.js +85 -3
- package/src/keys.js +12 -0
- package/src/naming.js +226 -0
- package/vendor/client/platform.js +184 -8
- package/vendor/client/session-store.js +32 -1
- package/vendor/desktop/lib/local/engine.js +78 -3
- package/vendor/desktop/lib/local/prompt.js +86 -2
- package/vendor/desktop/lib/local/tools.js +4 -1
package/src/clipboard.js
CHANGED
|
@@ -1,19 +1,49 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Cross-platform clipboard
|
|
5
|
-
*
|
|
6
|
-
* file in the data dir and reporting its path — the CLI never errors out over
|
|
7
|
-
* clipboard availability. Set AEGISCODE_NO_CLIPBOARD=1 to force the file
|
|
8
|
-
* fallback (used by tests and headless setups).
|
|
4
|
+
* Cross-platform clipboard access, both directions — the CLI's half of the
|
|
5
|
+
* gesture the user actually performs: mark text, copy it, paste it back.
|
|
9
6
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
7
|
+
* WRITE (copy): a real clipboard tool first (pbcopy / clip.exe / wl-copy /
|
|
8
|
+
* xclip / xsel), then **OSC 52** — the terminal's own "put this in the
|
|
9
|
+
* clipboard" escape, which needs no binary at all and works over SSH and inside
|
|
10
|
+
* tmux / screen — then a file in the data dir as the last resort. The CLI never
|
|
11
|
+
* errors out over clipboard availability.
|
|
12
|
+
*
|
|
13
|
+
* The OSC 52 rung is what makes copy work on a host with no clipboard binary
|
|
14
|
+
* installed. Measured on this host (`XDG_SESSION_TYPE=x11`, `DISPLAY=:0`, none
|
|
15
|
+
* of wl-copy / xclip / xsel present): every marking drag ended in
|
|
16
|
+
* "Marked N lines → ~/.aegiscode/clipboard.txt" — the text did NOT reach any
|
|
17
|
+
* clipboard, the user had to go find a file. The upstream engine fixed exactly
|
|
18
|
+
* this with the OSC 52 rung (`aegiscodex-dev/src/clipboard.js`); this module
|
|
19
|
+
* had the tool ladder and the file fallback only.
|
|
20
|
+
*
|
|
21
|
+
* READ (paste): the same tools in reverse — pbpaste / Get-Clipboard / xclip -o
|
|
22
|
+
* / wl-paste. Ctrl+V pastes an image from the clipboard (the file path lands in
|
|
23
|
+
* the prompt) or, when the clipboard holds no image, its text. Without this the
|
|
24
|
+
* CLI had NO clipboard paste at all: it only received what the terminal
|
|
25
|
+
* bracketed-paste frame delivered, so Ctrl+V did nothing on every platform.
|
|
26
|
+
*
|
|
27
|
+
* Two host-specific details this module has to get right, both of them
|
|
28
|
+
* previously wrong here:
|
|
29
|
+
*
|
|
30
|
+
* · **Windows has no mode bits.** The PATH hardening below (a directory is
|
|
31
|
+
* trusted only when it is not group/world-writable and is owned by root or
|
|
32
|
+
* by us) is a POSIX idea. `fs.statSync` on Windows reports 0o666-ish modes
|
|
33
|
+
* for every file, so `mode & 0o022` was true for *everything* — every
|
|
34
|
+
* candidate failed, `resolveTrustedTool()` returned null, and copy fell to
|
|
35
|
+
* the file rung on every Windows host even with clip.exe present. The
|
|
36
|
+
* ownership test is therefore skipped when `platform === 'win32'` (where the
|
|
37
|
+
* real guarantee is that PATH is user-owned), and Windows resolution adds
|
|
38
|
+
* the executable suffixes (`.exe`, `.cmd`, `.bat`) that a bare `clip` never
|
|
39
|
+
* matches.
|
|
40
|
+
* · **A tool that is not there is not a copy.** `via: 'file'` means the text
|
|
41
|
+
* reached no clipboard; `describeCopy()` says so instead of claiming a copy.
|
|
13
42
|
*
|
|
14
43
|
* Ported from the upstream engine reference (ESM → CommonJS). The data dir
|
|
15
|
-
* comes from the shared `aegisDir()` helper and the
|
|
16
|
-
*
|
|
44
|
+
* comes from the shared `aegisDir()` helper and the env vars are the CLI's own
|
|
45
|
+
* (AEGISCODE_*), with AEGISCODE_TRUST_PATH=0 and AEGISCODE_NO_OSC52=1 as the
|
|
46
|
+
* documented hatches.
|
|
17
47
|
*/
|
|
18
48
|
|
|
19
49
|
const { spawnSync } = require('node:child_process');
|
|
@@ -22,34 +52,105 @@ const path = require('node:path');
|
|
|
22
52
|
const { aegisDir } = require('./config.js');
|
|
23
53
|
const { writePrivateFile } = require('./securefs.js');
|
|
24
54
|
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
55
|
+
const ESC = '\u001b';
|
|
56
|
+
const realPlatform = () => process.platform;
|
|
57
|
+
|
|
58
|
+
// ── tool discovery ─────────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The clipboard tools this host could use, best-first.
|
|
62
|
+
*
|
|
63
|
+
* Platform gates first: pbcopy/pbpaste/pngpaste are macOS, clip.exe and
|
|
64
|
+
* PowerShell are Windows, the rest are X11/Wayland. Then env fit: on Linux a
|
|
65
|
+
* Wayland session lists wl-copy first and an X11 session lists xclip first, so
|
|
66
|
+
* a Wayland terminal with XWayland present doesn't pay a spawn + 4s timeout
|
|
67
|
+
* failing wl-copy. `platform` and `env` are injectable so every branch is
|
|
68
|
+
* exercisable on this Linux host (the repo's Phase 30 rule: no platform branch
|
|
69
|
+
* reachable only on the host it is written for).
|
|
70
|
+
*/
|
|
71
|
+
function clipboardCandidates(kind, { platform = realPlatform(), env = process.env } = {}) {
|
|
72
|
+
const isLinux = platform === 'linux';
|
|
73
|
+
const isMac = platform === 'darwin';
|
|
74
|
+
const isWin = platform === 'win32';
|
|
75
|
+
const e = env || {};
|
|
76
|
+
const hasWayland = !!e.WAYLAND_DISPLAY;
|
|
77
|
+
const linuxWrite = hasWayland
|
|
78
|
+
? [
|
|
79
|
+
{ name: 'wl-copy', cmd: 'wl-copy', args: [], env: 'wayland' },
|
|
80
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard'], env: 'x11' },
|
|
81
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--input'], env: 'x11' },
|
|
82
|
+
]
|
|
83
|
+
: [
|
|
84
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard'], env: 'x11' },
|
|
85
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--input'], env: 'x11' },
|
|
86
|
+
{ name: 'wl-copy', cmd: 'wl-copy', args: [], env: 'wayland' },
|
|
87
|
+
];
|
|
88
|
+
if (kind === 'write') {
|
|
89
|
+
return [
|
|
90
|
+
isMac && { name: 'pbcopy', cmd: 'pbcopy', args: [], env: 'any' },
|
|
91
|
+
isWin && { name: 'clip', cmd: 'clip', args: [], env: 'any' },
|
|
92
|
+
...(isLinux ? linuxWrite : []),
|
|
93
|
+
].filter(Boolean);
|
|
94
|
+
}
|
|
95
|
+
if (kind === 'text') {
|
|
96
|
+
return [
|
|
97
|
+
isMac && { name: 'pbpaste', cmd: 'pbpaste', args: [], env: 'any' },
|
|
98
|
+
isWin && {
|
|
99
|
+
name: 'powershell',
|
|
100
|
+
cmd: 'powershell',
|
|
101
|
+
args: ['-NoProfile', '-Command', 'Get-Clipboard'],
|
|
102
|
+
env: 'any',
|
|
103
|
+
winNewline: true,
|
|
104
|
+
},
|
|
105
|
+
...(isLinux ? [
|
|
106
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--no-newline'], env: 'wayland' },
|
|
107
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard', '-o'], env: 'x11' },
|
|
108
|
+
{ name: 'xsel', cmd: 'xsel', args: ['--clipboard', '--output'], env: 'x11' },
|
|
109
|
+
] : []),
|
|
110
|
+
].filter(Boolean);
|
|
111
|
+
}
|
|
112
|
+
// image
|
|
113
|
+
return [
|
|
114
|
+
isMac && { name: 'pngpaste', cmd: 'pngpaste', args: ['-'], env: 'any' },
|
|
115
|
+
...(isLinux ? [
|
|
116
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--type', 'image/png'], env: 'wayland', mime: 'image/png' },
|
|
117
|
+
{ name: 'wl-paste', cmd: 'wl-paste', args: ['--type', 'image/jpeg'], env: 'wayland', mime: 'image/jpeg' },
|
|
118
|
+
{ name: 'xclip', cmd: 'xclip', args: ['-selection', 'clipboard', '-t', 'image/png', '-o'], env: 'x11', mime: 'image/png' },
|
|
119
|
+
] : []),
|
|
120
|
+
].filter(Boolean);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ── PATH hardening (POSIX) and PATH resolution (Windows) ────────────────────
|
|
32
124
|
|
|
33
125
|
/**
|
|
34
126
|
* Is `dir` a directory a second local account could write into?
|
|
35
127
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* at the attacker instead of the clipboard.
|
|
128
|
+
* A clipboard tool is handed the complete text of whatever the user copied —
|
|
129
|
+
* which in this CLI is usually model output, sometimes containing code, paths,
|
|
130
|
+
* or a secret the user was about to paste into a browser. `spawnSync('wl-copy',
|
|
131
|
+
* …)` resolves that name against `$PATH`, so whoever owns an earlier PATH entry
|
|
132
|
+
* than the real binary owns every byte the user copies: drop a `wl-copy` in a
|
|
133
|
+
* group-writable `~/.local/bin`, or rely on `/tmp` or `.` being in PATH, and the
|
|
134
|
+
* text arrives at the attacker instead of the clipboard.
|
|
44
135
|
*
|
|
45
136
|
* A directory is trusted only when it is not group- or world-writable AND is
|
|
46
137
|
* owned by root or by us. `/tmp` fails on the first test; `~/bin` that the user
|
|
47
138
|
* owns and left at 0755 passes.
|
|
139
|
+
*
|
|
140
|
+
* On Windows neither test means anything: `stat` reports mode bits that do not
|
|
141
|
+
* describe the ACL, so `mode & 0o022` is true for every path and the check
|
|
142
|
+
* rejected the whole PATH (every copy fell through to the file rung). There the
|
|
143
|
+
* question "can a stranger swap this binary?" is answered by the ACL, not by
|
|
144
|
+
* `stat`, and the file this CLI would run is `clip.exe` out of
|
|
145
|
+
* `%SystemRoot%\system32` — so the ownership test is skipped and resolution is
|
|
146
|
+
* limited to directories on PATH plus the executable suffixes, which is what
|
|
147
|
+
* keeps `clip` from being confused with a `clip.cmd` sitting in the cwd.
|
|
48
148
|
*/
|
|
49
|
-
function dirIsTrusted(dir) {
|
|
149
|
+
function dirIsTrusted(dir, { platform = realPlatform() } = {}) {
|
|
50
150
|
try {
|
|
51
151
|
const st = fs.statSync(dir);
|
|
52
152
|
if (!st.isDirectory()) return false;
|
|
153
|
+
if (platform === 'win32') return true; // ACL-based; mode bits are not evidence
|
|
53
154
|
if (st.mode & 0o022) return false;
|
|
54
155
|
if (typeof process.getuid === 'function' && st.uid !== 0 && st.uid !== process.getuid()) return false;
|
|
55
156
|
return true;
|
|
@@ -58,6 +159,24 @@ function dirIsTrusted(dir) {
|
|
|
58
159
|
}
|
|
59
160
|
}
|
|
60
161
|
|
|
162
|
+
const WIN_EXTS = ['', '.exe', '.cmd', '.bat', '.com'];
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Join a PATH entry and a file name the way the *entry* is spelled.
|
|
166
|
+
*
|
|
167
|
+
* `path.win32.join('C:\\Windows', 'clip.exe')` is what Windows needs, but a
|
|
168
|
+
* win32-style join applied to a POSIX-style entry produces
|
|
169
|
+
* `\\tmp\\x\\clip.exe`, which no filesystem resolves — and the whole point of
|
|
170
|
+
* taking `platform` as an option is that the win32 branch is exercisable on
|
|
171
|
+
* this Linux host with a real temp directory. Windows accepts both separators,
|
|
172
|
+
* so choosing by the entry's own spelling is correct on both sides.
|
|
173
|
+
*/
|
|
174
|
+
function joinFor(dir, name, isWin) {
|
|
175
|
+
if (!isWin) return path.join(dir, name);
|
|
176
|
+
const winStyle = /\\/.test(dir) || /^[A-Za-z]:/.test(dir);
|
|
177
|
+
return winStyle ? path.win32.join(dir, name) : path.join(dir, name);
|
|
178
|
+
}
|
|
179
|
+
|
|
61
180
|
/**
|
|
62
181
|
* Resolve `cmd` to an absolute path we are willing to execute, or null.
|
|
63
182
|
*
|
|
@@ -68,59 +187,128 @@ function dirIsTrusted(dir) {
|
|
|
68
187
|
*
|
|
69
188
|
* Every candidate must survive: a PATH entry that is absolute, trusted, and not
|
|
70
189
|
* the empty string or `.` (both of which mean the current directory); a real
|
|
71
|
-
* file that is executable, not group/world-writable, and owned by root
|
|
72
|
-
* and a realpath whose own directory is trusted too, so a link in a good
|
|
73
|
-
* directory cannot redirect execution into a bad one.
|
|
190
|
+
* file that is executable (POSIX), not group/world-writable, and owned by root
|
|
191
|
+
* or us; and a realpath whose own directory is trusted too, so a link in a good
|
|
192
|
+
* directory cannot redirect execution into a bad one. On Windows the mode/owner
|
|
193
|
+
* tests are skipped (see `dirIsTrusted`) and each executable suffix is tried,
|
|
194
|
+
* because a bare `clip` is `clip.exe` on disk.
|
|
74
195
|
*/
|
|
75
|
-
function resolveTrustedTool(cmd) {
|
|
76
|
-
const
|
|
196
|
+
function resolveTrustedTool(cmd, { platform = realPlatform(), env = process.env } = {}) {
|
|
197
|
+
const e = env || {};
|
|
198
|
+
const trustPath = e.AEGISCODE_TRUST_PATH !== '0';
|
|
199
|
+
const isWin = platform === 'win32';
|
|
200
|
+
const p = isWin ? path.win32 : path;
|
|
201
|
+
const entries = String((e.PATH != null ? e.PATH : process.env.PATH) || '').split(isWin ? ';' : path.delimiter);
|
|
202
|
+
// AEGISCODE_TRUST_PATH=0 restores the old "whatever PATH resolves" behaviour
|
|
203
|
+
// for a user whose tool legitimately lives in a group-writable prefix (an
|
|
204
|
+
// unusual but real layout: a Nix/Guix profile, a CI image). Secure by
|
|
205
|
+
// default; the hatch is documented in the module header rather than being the
|
|
206
|
+
// default. It stays on for Windows, where the checks are ACL-based and
|
|
207
|
+
// `dirIsTrusted` has nothing to verify anyway.
|
|
208
|
+
const harden = trustPath || isWin;
|
|
77
209
|
for (const raw of entries) {
|
|
78
|
-
const dir = String(raw).trim();
|
|
79
|
-
if (!dir
|
|
80
|
-
if (!
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
real
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
st
|
|
92
|
-
|
|
93
|
-
|
|
210
|
+
const dir = String(raw).trim().replace(/^"|"$/g, '');
|
|
211
|
+
if (!dir) continue;
|
|
212
|
+
if (!isWin && !p.isAbsolute(dir)) continue; // '' and '.' mean "the cwd"
|
|
213
|
+
if (harden && !dirIsTrusted(dir, { platform })) continue;
|
|
214
|
+
|
|
215
|
+
for (const ext of isWin ? WIN_EXTS : ['']) {
|
|
216
|
+
const full = joinFor(dir, cmd + ext, isWin);
|
|
217
|
+
let real;
|
|
218
|
+
try {
|
|
219
|
+
real = fs.realpathSync(full); // throws on ENOENT and on a symlink loop
|
|
220
|
+
} catch {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
let st;
|
|
224
|
+
try {
|
|
225
|
+
st = fs.statSync(real);
|
|
226
|
+
} catch {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (!st.isFile()) continue;
|
|
230
|
+
if (!isWin) {
|
|
231
|
+
if (!(st.mode & 0o111)) continue; // not executable — always required
|
|
232
|
+
if (harden) {
|
|
233
|
+
if (st.mode & 0o022) continue; // group/world-writable binary
|
|
234
|
+
if (typeof process.getuid === 'function' && st.uid !== 0 && st.uid !== process.getuid()) continue;
|
|
235
|
+
if (!dirIsTrusted(path.dirname(real), { platform })) continue; // the realpath may leave `dir`
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return real;
|
|
94
239
|
}
|
|
95
|
-
if (!st.isFile()) continue;
|
|
96
|
-
if (!(st.mode & 0o111)) continue; // not executable
|
|
97
|
-
if (st.mode & 0o022) continue; // group/world-writable binary
|
|
98
|
-
if (typeof process.getuid === 'function' && st.uid !== 0 && st.uid !== process.getuid()) continue;
|
|
99
|
-
if (!dirIsTrusted(path.dirname(real))) continue; // the realpath may leave `dir`
|
|
100
|
-
return real;
|
|
101
240
|
}
|
|
102
241
|
return null;
|
|
103
242
|
}
|
|
104
243
|
|
|
244
|
+
// ── OSC 52 — the terminal's own clipboard ──────────────────────────────────
|
|
245
|
+
// "ESC ] 52 ; c ; <base64> BEL" sets the system clipboard. Inside tmux or
|
|
246
|
+
// screen the sequence has to be wrapped in a DCS passthrough or the
|
|
247
|
+
// multiplexer eats it (tmux forwards it with `set-clipboard` at its default).
|
|
248
|
+
// The inner ESC bytes are doubled for tmux, per its docs.
|
|
249
|
+
const base64 = (s) => Buffer.from(String(s), 'utf8').toString('base64');
|
|
250
|
+
/** Terminals that refuse OSC 52 for size need chunks, but the payloads a
|
|
251
|
+
* marking produces are small; a fixed cap keeps a giant copy from wedging the
|
|
252
|
+
* terminal, and the file rung already covers that case. */
|
|
253
|
+
const OSC52_MAX = 100_000;
|
|
254
|
+
|
|
255
|
+
function osc52Sequence(text, env = process.env) {
|
|
256
|
+
const body = `${ESC}]52;c;${base64(text)}\u0007`;
|
|
257
|
+
const e = env || {};
|
|
258
|
+
if (e.TMUX) return `${ESC}Ptmux;${ESC}${body.split(ESC).join(ESC + ESC)}${ESC}\\`;
|
|
259
|
+
if (e.STY) return `${ESC}P${body}${ESC}\\`;
|
|
260
|
+
return body;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Send `text` to the terminal's clipboard. Only meaningful on a TTY — with
|
|
265
|
+
* stdout redirected there is no terminal to hand it to, and the bytes would
|
|
266
|
+
* land in the redirected file (which is why `isTTY` is part of the contract and
|
|
267
|
+
* why the tests inject a stub stream). Returns true when the sequence was
|
|
268
|
+
* written.
|
|
269
|
+
*/
|
|
270
|
+
function writeOsc52(text, { stdout = process.stdout, env = process.env } = {}) {
|
|
271
|
+
const e = env || {};
|
|
272
|
+
if (e.AEGISCODE_NO_OSC52 === '1') return false;
|
|
273
|
+
if (!stdout || !stdout.isTTY || typeof stdout.write !== 'function') return false;
|
|
274
|
+
const payload = String(text);
|
|
275
|
+
if (!payload || payload.length > OSC52_MAX) return false;
|
|
276
|
+
try {
|
|
277
|
+
stdout.write(osc52Sequence(payload, e));
|
|
278
|
+
return true;
|
|
279
|
+
} catch {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ── copy ───────────────────────────────────────────────────────────────────
|
|
285
|
+
|
|
105
286
|
/**
|
|
106
287
|
* Copy text to the system clipboard.
|
|
107
|
-
* Returns { ok, via, path? } — via is 'tool:<name>' or 'file'.
|
|
288
|
+
* Returns { ok, via, path? } — via is 'tool:<name>', 'osc52:<where>' or 'file'.
|
|
289
|
+
* `via: 'file'` means the text did NOT reach any clipboard: the caller is
|
|
290
|
+
* expected to say so and hand over the path, not to claim a copy.
|
|
108
291
|
*/
|
|
109
|
-
function copyToClipboard(text) {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
292
|
+
function copyToClipboard(text, opts = {}) {
|
|
293
|
+
const {
|
|
294
|
+
stdout = process.stdout,
|
|
295
|
+
env = process.env,
|
|
296
|
+
platform = realPlatform(),
|
|
297
|
+
spawn = spawnSync,
|
|
298
|
+
} = opts;
|
|
299
|
+
const e = env || {};
|
|
300
|
+
const forced = e.AEGISCODE_NO_CLIPBOARD === '1';
|
|
116
301
|
if (!forced) {
|
|
117
|
-
for (const t of
|
|
118
|
-
|
|
119
|
-
const exe = trustPath ? resolveTrustedTool(t.cmd) : t.cmd;
|
|
302
|
+
for (const t of clipboardCandidates('write', { platform, env: e })) {
|
|
303
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
120
304
|
if (!exe) continue; // not installed, or installed somewhere untrusted
|
|
121
|
-
const r =
|
|
305
|
+
const r = spawn(exe, t.args, { input: String(text), encoding: 'utf8', timeout: 4000, windowsHide: true });
|
|
122
306
|
if (!r.error && r.status === 0) return { ok: true, via: `tool:${t.name}` };
|
|
123
307
|
}
|
|
308
|
+
if (writeOsc52(text, { stdout, env: e })) {
|
|
309
|
+
const where = e.TMUX ? 'tmux' : e.STY ? 'screen' : 'terminal';
|
|
310
|
+
return { ok: true, via: `osc52:${where}` };
|
|
311
|
+
}
|
|
124
312
|
}
|
|
125
313
|
// Fallback: write to the data dir and report the path. 0600 + O_NOFOLLOW via
|
|
126
314
|
// securefs — this file holds whatever the user last marked, and neither the
|
|
@@ -135,4 +323,99 @@ function copyToClipboard(text) {
|
|
|
135
323
|
}
|
|
136
324
|
}
|
|
137
325
|
|
|
138
|
-
|
|
326
|
+
/** Human sentence for a copy result — one place, so every caller matches. */
|
|
327
|
+
function describeCopy(res, subject = 'Copied') {
|
|
328
|
+
if (!res || !res.ok) return 'Clipboard unavailable; nothing copied.';
|
|
329
|
+
if (res.via === 'file') {
|
|
330
|
+
// Not a copy: nothing reached any clipboard, so the sentence must not say
|
|
331
|
+
// it did — it names the file the text actually went to instead.
|
|
332
|
+
return `${subject} — no clipboard tool on this host; the text is in ${res.path}`;
|
|
333
|
+
}
|
|
334
|
+
const [kind, detail] = String(res.via).split(':');
|
|
335
|
+
if (kind === 'osc52') {
|
|
336
|
+
return detail === 'terminal'
|
|
337
|
+
? `${subject} to the clipboard via the terminal.`
|
|
338
|
+
: `${subject} to the clipboard (through ${detail}).`;
|
|
339
|
+
}
|
|
340
|
+
return `${subject} to the clipboard (${detail}).`;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// ── read (paste) ───────────────────────────────────────────────────────────
|
|
344
|
+
|
|
345
|
+
const PNG_MAGIC = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
346
|
+
const JPEG_MAGIC = Buffer.from([0xff, 0xd8, 0xff]);
|
|
347
|
+
|
|
348
|
+
/** Sniff a Buffer's image type from its magic bytes, or null when not an image. */
|
|
349
|
+
function sniffImage(buf) {
|
|
350
|
+
if (!buf || buf.length < 8) return null;
|
|
351
|
+
if (buf.subarray(0, 8).equals(PNG_MAGIC)) return 'image/png';
|
|
352
|
+
if (buf.subarray(0, 3).equals(JPEG_MAGIC)) return 'image/jpeg';
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Read the clipboard's text, if any tool can. Returns { ok, text, via, error? }
|
|
358
|
+
* — `ok:false` means no tool answered, never "the clipboard was empty" (an
|
|
359
|
+
* empty clipboard returns ok:true and text:''). The caller's message differs for
|
|
360
|
+
* the two, which is the whole reason the distinction is in the return value.
|
|
361
|
+
*/
|
|
362
|
+
function readClipboardText(opts = {}) {
|
|
363
|
+
const { env = process.env, platform = realPlatform(), spawn = spawnSync } = opts;
|
|
364
|
+
const e = env || {};
|
|
365
|
+
if (e.AEGISCODE_NO_CLIPBOARD === '1') return { ok: false, via: 'none', error: 'clipboard disabled' };
|
|
366
|
+
for (const t of clipboardCandidates('text', { platform, env: e })) {
|
|
367
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
368
|
+
if (!exe) continue;
|
|
369
|
+
const r = spawn(exe, t.args, { encoding: 'utf8', timeout: 4000, maxBuffer: 16 * 1024 * 1024, windowsHide: true });
|
|
370
|
+
if (r.error || r.status !== 0 || r.stdout == null) continue;
|
|
371
|
+
// PowerShell's Get-Clipboard appends its own CRLF per line.
|
|
372
|
+
const text = t.winNewline
|
|
373
|
+
? String(r.stdout).replace(/\r\n/g, '\n').replace(/\n$/, '')
|
|
374
|
+
: String(r.stdout);
|
|
375
|
+
return { ok: true, text, via: `tool:${t.name}` };
|
|
376
|
+
}
|
|
377
|
+
return { ok: false, via: 'none', error: 'no clipboard reader available' };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Read an image off the clipboard. Returns { ok, data (Buffer), mime, via,
|
|
382
|
+
* error? }. The bytes are magic-checked: a tool that answers with text (an
|
|
383
|
+
* empty selection, a type the clipboard does not carry) is not an image, and
|
|
384
|
+
* the caller must not write it out as one.
|
|
385
|
+
*/
|
|
386
|
+
function readClipboardImage(opts = {}) {
|
|
387
|
+
const { env = process.env, platform = realPlatform(), spawn = spawnSync } = opts;
|
|
388
|
+
const e = env || {};
|
|
389
|
+
if (e.AEGISCODE_NO_CLIPBOARD === '1') return { ok: false, via: 'none', error: 'clipboard disabled' };
|
|
390
|
+
for (const t of clipboardCandidates('image', { platform, env: e })) {
|
|
391
|
+
const exe = resolveTrustedTool(t.cmd, { platform, env: e });
|
|
392
|
+
if (!exe) continue;
|
|
393
|
+
const r = spawn(exe, t.args, { timeout: 5000, maxBuffer: 64 * 1024 * 1024, windowsHide: true });
|
|
394
|
+
if (r.error || r.status !== 0 || !r.stdout) continue;
|
|
395
|
+
const data = Buffer.isBuffer(r.stdout) ? r.stdout : Buffer.from(String(r.stdout), 'utf8');
|
|
396
|
+
const mime = sniffImage(data);
|
|
397
|
+
if (mime) return { ok: true, data, mime, via: `tool:${t.name}` };
|
|
398
|
+
}
|
|
399
|
+
return { ok: false, via: 'none', error: 'no image on the clipboard' };
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Which clipboard facilities this host actually has — for honest UI hints. */
|
|
403
|
+
function clipboardCapabilities({ platform = realPlatform(), env = process.env } = {}) {
|
|
404
|
+
const any = (kind) =>
|
|
405
|
+
clipboardCandidates(kind, { platform, env }).some((t) => !!resolveTrustedTool(t.cmd, { platform, env }));
|
|
406
|
+
return { canWrite: any('write'), canReadText: any('text'), canReadImage: any('image') };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
module.exports = {
|
|
410
|
+
copyToClipboard,
|
|
411
|
+
describeCopy,
|
|
412
|
+
readClipboardText,
|
|
413
|
+
readClipboardImage,
|
|
414
|
+
sniffImage,
|
|
415
|
+
clipboardCandidates,
|
|
416
|
+
clipboardCapabilities,
|
|
417
|
+
osc52Sequence,
|
|
418
|
+
writeOsc52,
|
|
419
|
+
resolveTrustedTool,
|
|
420
|
+
dirIsTrusted,
|
|
421
|
+
};
|
package/src/events.js
CHANGED
|
@@ -67,7 +67,45 @@ const PASTE_DEBOUNCE_MS = 30;
|
|
|
67
67
|
let pasteChunks = [];
|
|
68
68
|
let pasteTimer = null;
|
|
69
69
|
|
|
70
|
+
// Any single escape sequence: a CSI (ESC [ … final), an OSC (ESC ] … BEL/ST) or
|
|
71
|
+
// a two-byte escape. Applied globally to strip control traffic out of a chunk
|
|
72
|
+
// so what remains can be judged as text.
|
|
73
|
+
const ESC_SEQUENCE_RE = /\x1b\[[0-9;?<]*[A-Za-z~]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\x1b[\s\S]/g;
|
|
74
|
+
// SGR mouse reports specifically (DECSET 1006): the bytes a drag is made of.
|
|
75
|
+
const SGR_MOUSE_RE = /\x1b\[<\d+;\d+;\d+[Mm]/g;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Is this chunk *only* control traffic — escape sequences and control bytes,
|
|
79
|
+
* with no text in it at all?
|
|
80
|
+
*
|
|
81
|
+
* This is the guard that made drag-to-copy work. DECSET 1002 reports one mouse
|
|
82
|
+
* cell per sweep, so a quick drag arrives as a single `data` event of ~15 bytes
|
|
83
|
+
* per report: eleven reports is 268 bytes, which crossed `PASTE_LARGE` and was
|
|
84
|
+
* classified as a PASTE. `flushPaste()` then dispatched ONE `{name:'paste'}`
|
|
85
|
+
* key — so `chatflow.js` never saw a single `drag`, nothing was ever marked,
|
|
86
|
+
* nothing was painted in reverse video, and the release arrived with no anchor
|
|
87
|
+
* (it became a click and copied nothing). Measured through the real pump on
|
|
88
|
+
* this host: a 268-byte swept read produced `SGR-7 present: false`, `input row:
|
|
89
|
+
* "❯ MARKER_ALPHA …"`, `release hint: undefined`. The same bug hits a burst of
|
|
90
|
+
* repeated arrow / function keys (20 × `\x1b[1;5A` = 120 bytes), which used to
|
|
91
|
+
* be typed into the editor as the literal text `[1;5A`.
|
|
92
|
+
*
|
|
93
|
+
* A chunk that mixes escapes with real text (`ESC[31mred` pasted from a
|
|
94
|
+
* colored log, or a mouse report that raced a genuine paste) is NOT control
|
|
95
|
+
* only, so it still takes the paste path.
|
|
96
|
+
*/
|
|
97
|
+
const isControlOnly = (s) => {
|
|
98
|
+
const str = String(s);
|
|
99
|
+
if (!str.includes('\x1b')) return false;
|
|
100
|
+
const rest = str.replace(ESC_SEQUENCE_RE, '').replace(/[\u0000-\u001f\u007f]/g, '');
|
|
101
|
+
return rest.trim() === '';
|
|
102
|
+
};
|
|
103
|
+
|
|
70
104
|
const looksLikePaste = (s) => {
|
|
105
|
+
// Mouse reports and key-repeat bursts are keystrokes, not pasted text — see
|
|
106
|
+
// isControlOnly. Checked first: size is not evidence when the chunk is all
|
|
107
|
+
// escape traffic.
|
|
108
|
+
if (isControlOnly(s)) return false;
|
|
71
109
|
if (s.length > PASTE_LARGE) return true;
|
|
72
110
|
// A trailing \n or \r\n is the ordinary end of a piped batch, not evidence
|
|
73
111
|
// of a paste; strip one terminator before looking for an embedded newline.
|
|
@@ -247,9 +285,14 @@ function flushPaste() {
|
|
|
247
285
|
pasteTimer = null;
|
|
248
286
|
// Strip any bracketed-paste framing that reached the buffer via a chunk
|
|
249
287
|
// split mid-frame, so literal [200~…[201~ can never leak into the editor.
|
|
288
|
+
// Mouse reports are stripped too: a report that raced a real paste into the
|
|
289
|
+
// same accumulate window (or a drag that started inside a paste's debounce)
|
|
290
|
+
// is not text, and typing `[<32;12;4M` into the prompt is the visible half of
|
|
291
|
+
// the bug the isControlOnly guard fixes.
|
|
250
292
|
const text = raw
|
|
251
293
|
.replace(/\x1b\[200~/g, '')
|
|
252
294
|
.replace(/\x1b\[201~/g, '')
|
|
295
|
+
.replace(SGR_MOUSE_RE, '')
|
|
253
296
|
.replace(/\r\n/g, '\n')
|
|
254
297
|
.replace(/\r/g, '\n')
|
|
255
298
|
.trimEnd();
|
|
@@ -327,6 +370,13 @@ function pushChars(s) {
|
|
|
327
370
|
pasteTimer = setTimeout(flushPaste, PASTE_DEBOUNCE_MS);
|
|
328
371
|
return;
|
|
329
372
|
}
|
|
373
|
+
// Control traffic is never paste text, not even inside an accumulate window:
|
|
374
|
+
// a drag that lands within a paste's 30ms debounce still has to arrive as
|
|
375
|
+
// drag reports, or the marking silently does nothing again.
|
|
376
|
+
if (isControlOnly(s)) {
|
|
377
|
+
feedChars(s);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
330
380
|
if (pasteChunks.length || looksLikePaste(s)) {
|
|
331
381
|
pasteChunks.push(s);
|
|
332
382
|
clearTimeout(pasteTimer);
|