cli-setu 0.3.2 → 0.3.4
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 +44 -40
- package/bin/setu.js +172 -149
- package/package.json +4 -1
- package/src/approvalRelay.js +73 -0
- package/src/cli-adapters/claude.js +400 -302
- package/src/cli-adapters/index.js +28 -27
- package/src/cli-adapters/shell.js +96 -88
- package/src/dedup.js +35 -0
- package/src/diagnostics.js +35 -0
- package/src/gitDiff.js +200 -0
- package/src/pair.js +162 -131
- package/src/providerProtocol.js +155 -0
- package/src/reconnect.js +94 -0
- package/src/sharedKey.js +40 -0
- package/src/status.js +1 -1
- package/src/stdinForwarder.js +33 -0
- package/src/supervisor.js +2234 -1369
- package/src/transportSplit.js +72 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Splits terminal text into pieces that each fit one realtime-provider message.
|
|
2
|
+
//
|
|
3
|
+
// Why: Ably's Free and Standard plans reject any single published message
|
|
4
|
+
// over 64 KiB (see Ably's published platform limits page). The CLI used
|
|
5
|
+
// to send each output batch and the whole reconnect snapshot as ONE message
|
|
6
|
+
// with no size limit, so a large chunk or a long history could be refused
|
|
7
|
+
// and never reach the phone. Encryption (about 1.33x for base64) and JSON
|
|
8
|
+
// escaping make the sent size differ from the text length, so sizes are
|
|
9
|
+
// MEASURED on the real message, not guessed.
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
// Well under 64 KiB (65,536 bytes) to leave room for the backend and
|
|
13
|
+
// provider envelope around our fields.
|
|
14
|
+
const MAX_MESSAGE_BYTES = 48_000;
|
|
15
|
+
// Azure Web PubSub allows 1 MB per message; well under it. Keeping Azure at
|
|
16
|
+
// its larger size means a history that used to arrive whole still arrives
|
|
17
|
+
// whole, so apps that do not know about parts keep working on Azure.
|
|
18
|
+
const AZURE_MESSAGE_BYTES = 900_000;
|
|
19
|
+
|
|
20
|
+
// provider is what the CLI is actually connected through ('ably' | 'azure').
|
|
21
|
+
// Unknown means the small, safe limit.
|
|
22
|
+
function limitForProvider(provider) {
|
|
23
|
+
return provider === 'azure' ? AZURE_MESSAGE_BYTES : MAX_MESSAGE_BYTES;
|
|
24
|
+
}
|
|
25
|
+
const MIN_PIECE_CHARS = 512;
|
|
26
|
+
|
|
27
|
+
// Never cut between the two halves of a surrogate pair: each piece is
|
|
28
|
+
// encrypted and decoded on its own, and a lone surrogate turns into U+FFFD.
|
|
29
|
+
function safeEnd(text, start, end) {
|
|
30
|
+
if (end >= text.length) return text.length;
|
|
31
|
+
const code = text.charCodeAt(end - 1);
|
|
32
|
+
if (code >= 0xd800 && code <= 0xdbff && end - start > 1) return end - 1;
|
|
33
|
+
return end;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// measure(piece) must return the byte size of the FULL message that would
|
|
37
|
+
// carry that piece (after encryption and JSON.stringify of the body).
|
|
38
|
+
// The first attempt is the WHOLE text (so anything that fits stays one part);
|
|
39
|
+
// when it does not fit, the size shrinks in proportion to how far over it was
|
|
40
|
+
// and is re-measured until the real message fits.
|
|
41
|
+
function splitIntoParts(text, { measure, maxBytes = MAX_MESSAGE_BYTES, initialChars = Infinity, minChars = MIN_PIECE_CHARS }) {
|
|
42
|
+
const parts = [];
|
|
43
|
+
let i = 0;
|
|
44
|
+
let size = Math.min(initialChars, text.length) || 1;
|
|
45
|
+
while (i < text.length) {
|
|
46
|
+
const end = safeEnd(text, i, Math.min(text.length, i + size));
|
|
47
|
+
const piece = text.slice(i, end);
|
|
48
|
+
const bytes = measure(piece);
|
|
49
|
+
if (bytes > maxBytes && piece.length > minChars) {
|
|
50
|
+
size = Math.max(minChars, Math.floor(piece.length * (maxBytes / bytes) * 0.9));
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
parts.push(piece);
|
|
54
|
+
i = end;
|
|
55
|
+
}
|
|
56
|
+
return parts;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Keeps the NEWEST output when a snapshot would be larger than maxChars.
|
|
60
|
+
// Cuts at a line boundary so the first kept line is whole, and says so with a
|
|
61
|
+
// visible notice instead of dropping history silently.
|
|
62
|
+
function trimSnapshot(text, maxChars) {
|
|
63
|
+
if (text.length <= maxChars) return { text, truncated: false, droppedChars: 0 };
|
|
64
|
+
let cut = text.length - maxChars;
|
|
65
|
+
const nl = text.indexOf('\n', cut);
|
|
66
|
+
if (nl !== -1 && nl < text.length - 1) cut = nl + 1;
|
|
67
|
+
const dropped = cut;
|
|
68
|
+
const notice = `\x1b[0m\r\n[cli-setu: ${dropped} characters of older output are not shown after a reconnect]\r\n`;
|
|
69
|
+
return { text: notice + text.slice(cut), truncated: true, droppedChars: dropped };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { MAX_MESSAGE_BYTES, AZURE_MESSAGE_BYTES, limitForProvider, splitIntoParts, trimSnapshot };
|