@causyne/agent 1.20.35 → 1.20.38

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/src/tui/text.js CHANGED
@@ -9,24 +9,21 @@
9
9
  // * Tool output was printed raw — see sanitize() below, which is a security
10
10
  // fix, not a cosmetic one.
11
11
  //
12
- // Pure JS. No dependency, no shell.
13
-
14
- import os from 'os';
15
-
12
+ // Pure TypeScript. No dependency, no shell.
13
+ import os from 'node:os';
16
14
  // CSI sequences (colour, cursor movement, erase) and OSC sequences (window
17
15
  // title, hyperlinks — terminated by BEL or ST).
18
16
  const CSI_PATTERN = /\x1b\[[0-?]*[ -/]*[@-~]/g;
19
17
  const OSC_PATTERN = /\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g;
20
18
  const OTHER_ESCAPE_PATTERN = /\x1b[@-Z\\-_]/g;
21
-
19
+ const OSC8_SEQUENCE = /^\x1b\]8;[^;]*;([^\x07\x1b]*)(\x07|\x1b\\)/;
22
20
  /** Everything that is an escape sequence rather than visible text. */
23
21
  export function stripAnsi(text) {
24
- return String(text == null ? '' : text)
25
- .replace(OSC_PATTERN, '')
26
- .replace(CSI_PATTERN, '')
27
- .replace(OTHER_ESCAPE_PATTERN, '');
22
+ return String(text == null ? '' : text)
23
+ .replace(OSC_PATTERN, '')
24
+ .replace(CSI_PATTERN, '')
25
+ .replace(OTHER_ESCAPE_PATTERN, '');
28
26
  }
29
-
30
27
  /**
31
28
  * Does this code point occupy two terminal columns?
32
29
  *
@@ -34,19 +31,16 @@ export function stripAnsi(text) {
34
31
  * makes a table of Indonesian and Chinese filenames drift apart.
35
32
  */
36
33
  function isWideCodePoint(code) {
37
- return (
38
- (code >= 0x1100 && code <= 0x115f) || // Hangul Jamo
39
- (code >= 0x2e80 && code <= 0xa4cf) || // CJK radicals … Yi
40
- (code >= 0xac00 && code <= 0xd7a3) || // Hangul syllables
41
- (code >= 0xf900 && code <= 0xfaff) || // CJK compatibility ideographs
42
- (code >= 0xfe30 && code <= 0xfe6f) || // CJK compatibility forms
43
- (code >= 0xff00 && code <= 0xff60) || // Fullwidth forms
44
- (code >= 0xffe0 && code <= 0xffe6) ||
45
- (code >= 0x1f300 && code <= 0x1f64f) || // emoji
46
- (code >= 0x1f900 && code <= 0x1f9ff)
47
- );
34
+ return ((code >= 0x1100 && code <= 0x115f) || // Hangul Jamo
35
+ (code >= 0x2e80 && code <= 0xa4cf) || // CJK radicals … Yi
36
+ (code >= 0xac00 && code <= 0xd7a3) || // Hangul syllables
37
+ (code >= 0xf900 && code <= 0xfaff) || // CJK compatibility ideographs
38
+ (code >= 0xfe30 && code <= 0xfe6f) || // CJK compatibility forms
39
+ (code >= 0xff00 && code <= 0xff60) || // Fullwidth forms
40
+ (code >= 0xffe0 && code <= 0xffe6) ||
41
+ (code >= 0x1f300 && code <= 0x1f64f) || // emoji
42
+ (code >= 0x1f900 && code <= 0x1f9ff));
48
43
  }
49
-
50
44
  /**
51
45
  * The East Asian AMBIGUOUS class (UAX #11).
52
46
  *
@@ -62,21 +56,19 @@ function isWideCodePoint(code) {
62
56
  * that must not be wrong get the wide reading via the environment.
63
57
  */
64
58
  function isAmbiguousCodePoint(code) {
65
- return (
66
- (code >= 0x00a1 && code <= 0x00ff && code !== 0x00ad) ||
67
- (code >= 0x2010 && code <= 0x2027) ||
68
- code === 0x2022 || code === 0x2026 ||
69
- (code >= 0x2190 && code <= 0x21ff) || // arrows
70
- (code >= 0x2200 && code <= 0x22ff) || // mathematical operators
71
- (code >= 0x2460 && code <= 0x24ff) || // enclosed alphanumerics
72
- (code >= 0x2500 && code <= 0x257f) || // box drawing
73
- (code >= 0x2580 && code <= 0x259f) || // block elements
74
- (code >= 0x25a0 && code <= 0x25ff) || // geometric shapes
75
- (code >= 0x2600 && code <= 0x26ff) || // miscellaneous symbols
76
- (code >= 0x2700 && code <= 0x27bf) // dingbats (✓ ✕)
77
- );
59
+ return ((code >= 0x00a1 && code <= 0x00ff && code !== 0x00ad) ||
60
+ (code >= 0x2010 && code <= 0x2027) ||
61
+ code === 0x2022 || code === 0x2026 ||
62
+ (code >= 0x2190 && code <= 0x21ff) || // arrows
63
+ (code >= 0x2200 && code <= 0x22ff) || // mathematical operators
64
+ (code >= 0x2460 && code <= 0x24ff) || // enclosed alphanumerics
65
+ (code >= 0x2500 && code <= 0x257f) || // box drawing
66
+ (code >= 0x2580 && code <= 0x259f) || // block elements
67
+ (code >= 0x25a0 && code <= 0x25ff) || // geometric shapes
68
+ (code >= 0x2600 && code <= 0x26ff) || // miscellaneous symbols
69
+ (code >= 0x2700 && code <= 0x27bf) // dingbats (✓ ✕)
70
+ );
78
71
  }
79
-
80
72
  /**
81
73
  * Should ambiguous-width code points be measured as two columns?
82
74
  *
@@ -85,135 +77,141 @@ function isAmbiguousCodePoint(code) {
85
77
  * renders these glyphs wide.
86
78
  */
87
79
  function detectAmbiguousWide() {
88
- const explicit = process.env.CAUSYNE_AMBIGUOUS_WIDE;
89
- if (explicit === '1') return true;
90
- if (explicit === '0') return false;
91
- const locale = String(process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG || '');
92
- return /^(ja|ko|zh)([_.-]|$)/i.test(locale);
80
+ const explicit = process.env.CAUSYNE_AMBIGUOUS_WIDE;
81
+ if (explicit === '1')
82
+ return true;
83
+ if (explicit === '0')
84
+ return false;
85
+ const locale = String(process.env.LC_ALL || process.env.LC_CTYPE || process.env.LANG || '');
86
+ return /^(ja|ko|zh)([_.-]|$)/i.test(locale);
93
87
  }
94
-
95
88
  export const AMBIGUOUS_IS_WIDE = detectAmbiguousWide();
96
-
97
89
  /** Columns one code point occupies, under the active ambiguous-width policy. */
98
90
  function columnsFor(code) {
99
- if (isWideCodePoint(code)) return 2;
100
- if (AMBIGUOUS_IS_WIDE && isAmbiguousCodePoint(code)) return 2;
101
- return 1;
91
+ if (isWideCodePoint(code))
92
+ return 2;
93
+ if (AMBIGUOUS_IS_WIDE && isAmbiguousCodePoint(code))
94
+ return 2;
95
+ return 1;
102
96
  }
103
-
104
97
  /** Columns this string will occupy once printed. Escapes count for nothing. */
105
98
  export function visibleWidth(text) {
106
- const plain = stripAnsi(text);
107
- let width = 0;
108
- for (const character of plain) {
109
- const code = character.codePointAt(0);
110
- if (code === 0x200d) continue; // zero-width joiner
111
- if (code >= 0xfe00 && code <= 0xfe0f) continue; // variation selectors
112
- width += columnsFor(code);
113
- }
114
- return width;
99
+ const plain = stripAnsi(text);
100
+ let width = 0;
101
+ for (const character of plain) {
102
+ const code = character.codePointAt(0);
103
+ if (code === undefined)
104
+ continue;
105
+ if (code === 0x200d)
106
+ continue; // zero-width joiner
107
+ if (code >= 0xfe00 && code <= 0xfe0f)
108
+ continue; // variation selectors
109
+ width += columnsFor(code);
110
+ }
111
+ return width;
115
112
  }
116
-
117
113
  const RESET = '\x1b[0m';
118
-
119
114
  /**
120
115
  * Cut to `width` columns without ever splitting an escape sequence.
121
116
  *
122
117
  * Walks the string escape-aware rather than slicing by index, and closes any
123
- * style that was still open at the cut so the colour does not bleed into the
124
- * next line.
118
+ * style or OSC 8 hyperlink that was still open at the cut so neither colour nor
119
+ * link underline bleeds into the rest of the row or subsequent rows.
125
120
  */
126
121
  export function truncate(text, width, ellipsis = '…') {
127
- const source = String(text == null ? '' : text);
128
- if (width <= 0) return '';
129
- if (visibleWidth(source) <= width) return source;
130
-
131
- const ellipsisWidth = visibleWidth(ellipsis);
132
- const budget = Math.max(0, width - ellipsisWidth);
133
-
134
- let out = '';
135
- let used = 0;
136
- let sawEscape = false;
137
- let index = 0;
138
-
139
- while (index < source.length) {
140
- if (source[index] === '\x1b') {
141
- // Copy the whole sequence verbatim; it costs no columns.
142
- const rest = source.slice(index);
143
- const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
144
- || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
145
- || rest.match(/^\x1b[@-Z\\-_]/);
146
- if (match) {
147
- out += match[0];
148
- sawEscape = true;
149
- index += match[0].length;
150
- continue;
151
- }
122
+ const source = String(text == null ? '' : text);
123
+ if (width <= 0)
124
+ return '';
125
+ if (visibleWidth(source) <= width)
126
+ return source;
127
+ const ellipsisWidth = visibleWidth(ellipsis);
128
+ const budget = Math.max(0, width - ellipsisWidth);
129
+ let out = '';
130
+ let used = 0;
131
+ let sawEscape = false;
132
+ let openOsc8Term = '';
133
+ let index = 0;
134
+ while (index < source.length) {
135
+ if (source[index] === '\x1b') {
136
+ // Copy the whole sequence verbatim; it costs no columns.
137
+ const rest = source.slice(index);
138
+ const match = rest.match(/^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/)
139
+ || rest.match(/^\x1b\[[0-?]*[ -/]*[@-~]/)
140
+ || rest.match(/^\x1b[@-Z\\-_]/);
141
+ if (match) {
142
+ const osc8 = OSC8_SEQUENCE.exec(match[0]);
143
+ if (osc8) {
144
+ const uri = osc8[1] ?? '';
145
+ const term = osc8[2] ?? '\x1b\\';
146
+ openOsc8Term = uri.length > 0 ? term : '';
147
+ }
148
+ out += match[0];
149
+ sawEscape = true;
150
+ index += match[0].length;
151
+ continue;
152
+ }
153
+ }
154
+ const code = source.codePointAt(index);
155
+ if (code === undefined)
156
+ break;
157
+ const character = String.fromCodePoint(code);
158
+ const charWidth = columnsFor(code);
159
+ if (used + charWidth > budget)
160
+ break;
161
+ out += character;
162
+ used += charWidth;
163
+ index += character.length;
152
164
  }
153
-
154
- const character = String.fromCodePoint(source.codePointAt(index));
155
- const charWidth = columnsFor(source.codePointAt(index));
156
- if (used + charWidth > budget) break;
157
-
158
- out += character;
159
- used += charWidth;
160
- index += character.length;
161
- }
162
-
163
- return out + (sawEscape ? RESET : '') + ellipsis;
165
+ const osc8Reset = openOsc8Term ? `\x1b]8;;${openOsc8Term}` : '';
166
+ return out + osc8Reset + (sawEscape ? RESET : '') + ellipsis;
164
167
  }
165
-
166
168
  /** Pad to an exact column count, measuring visible width rather than bytes. */
167
169
  export function padEnd(text, width) {
168
- const gap = width - visibleWidth(text);
169
- return gap > 0 ? String(text) + ' '.repeat(gap) : String(text);
170
+ const gap = width - visibleWidth(text);
171
+ return gap > 0 ? String(text) + ' '.repeat(gap) : String(text);
170
172
  }
171
-
172
-
173
- export function wrap(text, width) {
174
- if (width <= 0) return [String(text)];
175
- const lines = [];
176
-
177
- for (const paragraph of String(text == null ? '' : text).split('\n')) {
178
- if (visibleWidth(paragraph) <= width) {
179
- lines.push(paragraph);
180
- continue;
181
- }
182
-
183
- let current = '';
184
- for (const word of paragraph.split(' ')) {
185
- const candidate = current ? `${current} ${word}` : word;
186
-
187
- if (visibleWidth(candidate) <= width) {
188
- current = candidate;
189
- continue;
190
- }
191
- if (current) {
192
- lines.push(current);
193
- current = '';
194
- }
195
-
196
- let remainder = word;
197
- while (visibleWidth(remainder) > width) {
198
- lines.push(truncate(remainder, width, ''));
199
- // truncate() gives us the head; drop the same number of characters.
200
- let consumed = 0;
201
- let cut = 0;
202
- while (cut < remainder.length && consumed < width) {
203
- const code = remainder.codePointAt(cut);
204
- consumed += columnsFor(code);
205
- cut += String.fromCodePoint(code).length;
173
+ export function wrap(text, width, _options) {
174
+ if (width <= 0)
175
+ return [String(text)];
176
+ const lines = [];
177
+ for (const paragraph of String(text == null ? '' : text).split('\n')) {
178
+ if (visibleWidth(paragraph) <= width) {
179
+ lines.push(paragraph);
180
+ continue;
206
181
  }
207
- remainder = remainder.slice(cut);
208
- }
209
- current = remainder;
182
+ let current = '';
183
+ for (const word of paragraph.split(' ')) {
184
+ const candidate = current ? `${current} ${word}` : word;
185
+ if (visibleWidth(candidate) <= width) {
186
+ current = candidate;
187
+ continue;
188
+ }
189
+ if (current) {
190
+ lines.push(current);
191
+ current = '';
192
+ }
193
+ let remainder = word;
194
+ while (visibleWidth(remainder) > width) {
195
+ lines.push(truncate(remainder, width, ''));
196
+ // truncate() gives us the head; drop the same number of characters.
197
+ let consumed = 0;
198
+ let cut = 0;
199
+ while (cut < remainder.length && consumed < width) {
200
+ const code = remainder.codePointAt(cut);
201
+ if (code === undefined)
202
+ break;
203
+ consumed += columnsFor(code);
204
+ cut += String.fromCodePoint(code).length;
205
+ }
206
+ remainder = remainder.slice(cut);
207
+ }
208
+ current = remainder;
209
+ }
210
+ if (current)
211
+ lines.push(current);
210
212
  }
211
- if (current) lines.push(current);
212
- }
213
-
214
- return lines;
213
+ return lines;
215
214
  }
216
-
217
215
  /**
218
216
  * Word-wrap a block of text while preserving each line's own indentation.
219
217
  *
@@ -226,23 +224,25 @@ export function wrap(text, width) {
226
224
  * terminal broke it at the right edge, mid-word, at column 0.
227
225
  */
228
226
  export function wrapBlock(text, width, { hang = 2 } = {}) {
229
- const rows = [];
230
- for (const line of String(text == null ? '' : text).split('\n')) {
231
- const indent = (line.match(/^[ \t]*/) || [''])[0];
232
- const body = line.slice(indent.length);
233
- if (!body) { rows.push(''); continue; }
234
-
235
- const continuation = indent + ' '.repeat(hang);
236
- // Wrap at the NARROWER of the two budgets so neither the first row nor a
237
- // continuation row can exceed `width`.
238
- const budget = Math.max(8, width - visibleWidth(continuation));
239
- const parts = wrap(body, budget);
240
- rows.push(indent + parts[0]);
241
- for (const part of parts.slice(1)) rows.push(continuation + part);
242
- }
243
- return rows;
227
+ const rows = [];
228
+ for (const line of String(text == null ? '' : text).split('\n')) {
229
+ const indent = (line.match(/^[ \t]*/) || [''])[0];
230
+ const body = line.slice(indent.length);
231
+ if (!body) {
232
+ rows.push('');
233
+ continue;
234
+ }
235
+ const continuation = indent + ' '.repeat(hang);
236
+ // Wrap at the NARROWER of the two budgets so neither the first row nor a
237
+ // continuation row can exceed `width`.
238
+ const budget = Math.max(8, width - visibleWidth(continuation));
239
+ const parts = wrap(body, budget);
240
+ rows.push(indent + (parts[0] ?? ''));
241
+ for (const part of parts.slice(1))
242
+ rows.push(continuation + part);
243
+ }
244
+ return rows;
244
245
  }
245
-
246
246
  /**
247
247
  * Make untrusted text safe to print.
248
248
  *
@@ -257,7 +257,6 @@ export function wrapBlock(text, width, { hang = 2 } = {}) {
257
257
  */
258
258
  /** Columns between terminal tab stops. Eight is the POSIX default everywhere. */
259
259
  const TAB_WIDTH = 8;
260
-
261
260
  /**
262
261
  * Replace tabs with the spaces the terminal would have moved over.
263
262
  *
@@ -283,66 +282,63 @@ const TAB_WIDTH = 8;
283
282
  * columnar output actually lining up.
284
283
  */
285
284
  export function expandTabs(text, tabWidth = TAB_WIDTH) {
286
- const source = String(text == null ? '' : text);
287
- if (!source.includes('\t')) return source;
288
-
289
- let out = '';
290
- let column = 0;
291
- for (const char of source) {
292
- if (char === '\t') {
293
- const advance = tabWidth - (column % tabWidth);
294
- out += ' '.repeat(advance);
295
- column += advance;
296
- } else if (char === '\n') {
297
- out += char;
298
- column = 0;
299
- } else {
300
- out += char;
301
- column += 1;
285
+ const source = String(text == null ? '' : text);
286
+ if (!source.includes('\t'))
287
+ return source;
288
+ let out = '';
289
+ let column = 0;
290
+ for (const char of source) {
291
+ if (char === '\t') {
292
+ const advance = tabWidth - (column % tabWidth);
293
+ out += ' '.repeat(advance);
294
+ column += advance;
295
+ }
296
+ else if (char === '\n') {
297
+ out += char;
298
+ column = 0;
299
+ }
300
+ else {
301
+ out += char;
302
+ column += 1;
303
+ }
302
304
  }
303
- }
304
- return out;
305
+ return out;
305
306
  }
306
-
307
307
  export function sanitize(text) {
308
- // Tabs are expanded rather than stripped: removing them would collapse
309
- // columnar output into an unreadable run, and leaving them corrupts the
310
- // frame. Expansion is the only option that preserves what the user meant to
311
- // see. Everything else in this class is genuinely unwanted and is removed.
312
- const normalized = String(text == null ? '' : text)
313
- .replace(/\r\n/g, '\n')
314
- .replace(/\r/g, '\n');
315
- return expandTabs(stripAnsi(normalized))
316
- // eslint-disable-next-line no-control-regex
317
- .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
308
+ // Tabs are expanded rather than stripped: removing them would collapse
309
+ // columnar output into an unreadable run, and leaving them corrupts the
310
+ // frame. Expansion is the only option that preserves what the user meant to
311
+ // see. Everything else in this class is genuinely unwanted and is removed.
312
+ const normalized = String(text == null ? '' : text)
313
+ .replace(/\r\n/g, '\n')
314
+ .replace(/\r/g, '\n');
315
+ return expandTabs(stripAnsi(normalized))
316
+ // eslint-disable-next-line no-control-regex
317
+ .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
318
318
  }
319
-
320
319
  /** `/Users/example/Projects/x` → `~/Projects/x`. Long paths dominate a tool row. */
321
320
  export function shortenHome(value) {
322
- const home = os.homedir();
323
- const text = String(value == null ? '' : value);
324
- if (home && text.startsWith(home)) return '~' + text.slice(home.length);
325
- return text;
321
+ const home = os.homedir();
322
+ const text = String(value == null ? '' : value);
323
+ if (home && text.startsWith(home))
324
+ return '~' + text.slice(home.length);
325
+ return text;
326
326
  }
327
-
328
-
329
327
  const SECRET_PATTERNS = [
330
- [/\b(sk|pk|rk)-[A-Za-z0-9_-]{12,}/g, (m) => `${m.slice(0, 5)}…${m.slice(-4)}`],
331
- [/\b(gh[pousr]|xox[baprs])_[A-Za-z0-9]{12,}/g, (m) => `${m.slice(0, 7)}…`],
332
- [/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{12,}/gi, (m) => `${m.split(/\s+/)[0]} …`],
333
- [/\b(api[_-]?key|token|secret|password|passwd)\s*[=:]\s*["']?[^\s"']{6,}/gi,
334
- (m) => `${m.split(/[=:]/)[0]}=…`]
328
+ [/\b(sk|pk|rk)-[A-Za-z0-9_-]{12,}/g, (m) => `${m.slice(0, 5)}…${m.slice(-4)}`],
329
+ [/\b(gh[pousr]|xox[baprs])_[A-Za-z0-9]{12,}/g, (m) => `${m.slice(0, 7)}…`],
330
+ [/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{12,}/gi, (m) => `${m.split(/\s+/)[0]} …`],
331
+ [/\b(api[_-]?key|token|secret|password|passwd)\s*[=:]\s*["']?[^\s"']{6,}/gi,
332
+ (m) => `${m.split(/[=:]/)[0]}=…`]
335
333
  ];
336
-
337
334
  export function redactSecrets(text) {
338
- let out = String(text == null ? '' : text);
339
- for (const [pattern, replace] of SECRET_PATTERNS) {
340
- out = out.replace(pattern, replace);
341
- }
342
- return out;
335
+ let out = String(text == null ? '' : text);
336
+ for (const [pattern, replace] of SECRET_PATTERNS) {
337
+ out = out.replace(pattern, replace);
338
+ }
339
+ return out;
343
340
  }
344
-
345
341
  /** sanitize + redact — the pipeline every untrusted string goes through. */
346
342
  export function safeDisplay(text) {
347
- return redactSecrets(sanitize(text));
343
+ return redactSecrets(sanitize(text));
348
344
  }