openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1,213 lines (1,211 loc) • 23.2 kB
JavaScript
//#region node_modules/.pnpm/ansi-regex@6.4.0/node_modules/ansi-regex/index.js
function ansiRegex({ onlyFirst: e = false } = {}) {
return new RegExp(`(?:(?:\\u001B\\]|\\u009D)[^\\u0007\\u001B\\u009C\\u009D]*(?:\\u0007|\\u001B\\u005C|\\u009C))|[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]`, e ? void 0 : "g");
}
//#endregion
//#region node_modules/.pnpm/strip-ansi@7.2.0/node_modules/strip-ansi/index.js
const regex = ansiRegex();
function stripAnsi$1(e) {
if (typeof e !== "string") throw new TypeError(`Expected a \`string\`, got \`${typeof e}\``);
if (!e.includes("\x1B") && !e.includes("")) return e;
return e.replace(regex, "");
}
//#endregion
//#region node_modules/.pnpm/get-east-asian-width@1.7.0/node_modules/get-east-asian-width/lookup-data.js
const ambiguousMaximumCodePoint = 1114109;
const ambiguousRanges = [
161,
161,
164,
164,
167,
168,
170,
170,
173,
174,
176,
180,
182,
186,
188,
191,
198,
198,
208,
208,
215,
216,
222,
225,
230,
230,
232,
234,
236,
237,
240,
240,
242,
243,
247,
250,
252,
252,
254,
254,
257,
257,
273,
273,
275,
275,
283,
283,
294,
295,
299,
299,
305,
307,
312,
312,
319,
322,
324,
324,
328,
331,
333,
333,
338,
339,
358,
359,
363,
363,
462,
462,
464,
464,
466,
466,
468,
468,
470,
470,
472,
472,
474,
474,
476,
476,
593,
593,
609,
609,
708,
708,
711,
711,
713,
715,
717,
717,
720,
720,
728,
731,
733,
733,
735,
735,
768,
879,
913,
929,
931,
937,
945,
961,
963,
969,
1025,
1025,
1040,
1103,
1105,
1105,
8208,
8208,
8211,
8214,
8216,
8217,
8220,
8221,
8224,
8226,
8228,
8231,
8240,
8240,
8242,
8243,
8245,
8245,
8251,
8251,
8254,
8254,
8308,
8308,
8319,
8319,
8321,
8324,
8364,
8364,
8451,
8451,
8453,
8453,
8457,
8457,
8467,
8467,
8470,
8470,
8481,
8482,
8486,
8486,
8491,
8491,
8531,
8532,
8539,
8542,
8544,
8555,
8560,
8569,
8585,
8585,
8592,
8601,
8632,
8633,
8658,
8658,
8660,
8660,
8679,
8679,
8704,
8704,
8706,
8707,
8711,
8712,
8715,
8715,
8719,
8719,
8721,
8721,
8725,
8725,
8730,
8730,
8733,
8736,
8739,
8739,
8741,
8741,
8743,
8748,
8750,
8750,
8756,
8759,
8764,
8765,
8776,
8776,
8780,
8780,
8786,
8786,
8800,
8801,
8804,
8807,
8810,
8811,
8814,
8815,
8834,
8835,
8838,
8839,
8853,
8853,
8857,
8857,
8869,
8869,
8895,
8895,
8978,
8978,
9312,
9449,
9451,
9547,
9552,
9587,
9600,
9615,
9618,
9621,
9632,
9633,
9635,
9641,
9650,
9651,
9654,
9655,
9660,
9661,
9664,
9665,
9670,
9672,
9675,
9675,
9678,
9681,
9698,
9701,
9711,
9711,
9733,
9734,
9737,
9737,
9742,
9743,
9756,
9756,
9758,
9758,
9792,
9792,
9794,
9794,
9824,
9825,
9827,
9829,
9831,
9834,
9836,
9837,
9839,
9839,
9886,
9887,
9919,
9919,
9926,
9933,
9935,
9939,
9941,
9953,
9955,
9955,
9960,
9961,
9963,
9969,
9972,
9972,
9974,
9977,
9979,
9980,
9982,
9983,
10045,
10045,
10102,
10111,
11094,
11097,
12872,
12879,
57344,
63743,
65024,
65039,
65533,
65533,
127232,
127242,
127248,
127277,
127280,
127337,
127344,
127373,
127375,
127376,
127387,
127404,
917760,
917999,
983040,
1048573,
1048576,
1114109
];
const fullwidthMinimalCodePoint = 12288;
const fullwidthMaximumCodePoint = 65510;
const fullwidthRanges = [
12288,
12288,
65281,
65376,
65504,
65510
];
const halfwidthMinimalCodePoint = 8361;
const halfwidthMaximumCodePoint = 65518;
const halfwidthRanges = [
8361,
8361,
65377,
65470,
65474,
65479,
65482,
65487,
65490,
65495,
65498,
65500,
65512,
65518
];
const narrowMaximumCodePoint = 10630;
const narrowRanges = [
32,
126,
162,
163,
165,
166,
172,
172,
175,
175,
10214,
10221,
10629,
10630
];
const wideMinimalCodePoint = 4352;
const wideMaximumCodePoint = 262141;
const wideRanges = [
4352,
4447,
8986,
8987,
9001,
9002,
9193,
9196,
9200,
9200,
9203,
9203,
9725,
9726,
9748,
9749,
9776,
9783,
9800,
9811,
9855,
9855,
9866,
9871,
9875,
9875,
9889,
9889,
9898,
9899,
9917,
9918,
9924,
9925,
9934,
9934,
9940,
9940,
9962,
9962,
9970,
9971,
9973,
9973,
9978,
9978,
9981,
9981,
9989,
9989,
9994,
9995,
10024,
10024,
10060,
10060,
10062,
10062,
10067,
10069,
10071,
10071,
10133,
10135,
10160,
10160,
10175,
10175,
11035,
11036,
11088,
11088,
11093,
11093,
11904,
11929,
11931,
12019,
12032,
12245,
12272,
12287,
12289,
12350,
12353,
12438,
12441,
12543,
12549,
12591,
12593,
12686,
12688,
12773,
12783,
12830,
12832,
12871,
12880,
42124,
42128,
42182,
43360,
43388,
44032,
55203,
63744,
64255,
65040,
65049,
65072,
65106,
65108,
65126,
65128,
65131,
94176,
94180,
94192,
94198,
94208,
101594,
101631,
101664,
101760,
101874,
101888,
102801,
102816,
102866,
110576,
110579,
110581,
110587,
110589,
110590,
110592,
110888,
110898,
110898,
110928,
110930,
110933,
110933,
110948,
110952,
110960,
111355,
119552,
119638,
119648,
119670,
126980,
126980,
127183,
127183,
127374,
127374,
127377,
127386,
127406,
127406,
127488,
127490,
127504,
127547,
127552,
127560,
127568,
127569,
127584,
127589,
127744,
127776,
127789,
127797,
127799,
127868,
127870,
127891,
127904,
127946,
127951,
127955,
127968,
127984,
127988,
127988,
127992,
128062,
128064,
128064,
128066,
128252,
128255,
128317,
128331,
128334,
128336,
128359,
128378,
128378,
128405,
128406,
128420,
128420,
128507,
128591,
128640,
128709,
128716,
128716,
128720,
128722,
128725,
128729,
128732,
128735,
128747,
128748,
128756,
128764,
128986,
128986,
128992,
129003,
129008,
129008,
129292,
129338,
129340,
129349,
129351,
129535,
129648,
129660,
129664,
129734,
129736,
129736,
129740,
129757,
129759,
129771,
129775,
129786,
131072,
196605,
196608,
262141
];
//#endregion
//#region node_modules/.pnpm/get-east-asian-width@1.7.0/node_modules/get-east-asian-width/utilities.js
/**
Binary search on a sorted flat array of [start, end] pairs.
@param {number[]} ranges - Flat array of inclusive [start, end] range pairs, e.g. [0, 5, 10, 20].
@param {number} codePoint - The value to search for.
@returns {boolean} Whether the value falls within any of the ranges.
*/
const isInRange = (e, t) => {
let n = 0;
let r = Math.floor(e.length / 2) - 1;
while (n <= r) {
const i = Math.floor((n + r) / 2);
const a = i * 2;
if (t < e[a]) r = i - 1;
else if (t > e[a + 1]) n = i + 1;
else return true;
}
return false;
};
//#endregion
//#region node_modules/.pnpm/get-east-asian-width@1.7.0/node_modules/get-east-asian-width/lookup.js
const commonCjkCodePoint = 19968;
const [wideFastPathStart, wideFastPathEnd] = /* #__PURE__ */ findWideFastPathRange(wideRanges);
function findWideFastPathRange(e) {
let t = e[0];
let n = e[1];
for (let r = 0; r < e.length; r += 2) {
const i = e[r];
const a = e[r + 1];
if (commonCjkCodePoint >= i && commonCjkCodePoint <= a) return [i, a];
if (a - i > n - t) {
t = i;
n = a;
}
}
return [t, n];
}
const isAmbiguous = (e) => {
if (e < 161 || e > 1114109) return false;
return isInRange(ambiguousRanges, e);
};
const isFullwidth = (e) => {
if (e < 12288 || e > 65510) return false;
return isInRange(fullwidthRanges, e);
};
const isHalfwidth = (e) => {
if (e < 8361 || e > 65518) return false;
return isInRange(halfwidthRanges, e);
};
const isNarrow = (e) => {
if (e < 32 || e > 10630) return false;
return isInRange(narrowRanges, e);
};
const isWide = (e) => {
if (e >= wideFastPathStart && e <= wideFastPathEnd) return true;
if (e < 4352 || e > 262141) return false;
return isInRange(wideRanges, e);
};
function getCategory(e) {
if (isAmbiguous(e)) return "ambiguous";
if (isFullwidth(e)) return "fullwidth";
if (isHalfwidth(e)) return "halfwidth";
if (isNarrow(e)) return "narrow";
if (isWide(e)) return "wide";
return "neutral";
}
//#endregion
//#region node_modules/.pnpm/get-east-asian-width@1.7.0/node_modules/get-east-asian-width/index.js
function validate(e) {
if (!Number.isSafeInteger(e)) throw new TypeError(`Expected a code point, got \`${typeof e}\`.`);
}
function eastAsianWidthType(e) {
validate(e);
return getCategory(e);
}
function eastAsianWidth(e, { ambiguousAsWide: t = false } = {}) {
validate(e);
if (isFullwidth(e) || isWide(e) || t && isAmbiguous(e)) return 2;
return 1;
}
//#endregion
//#region node_modules/.pnpm/string-width@8.3.0/node_modules/string-width/index.js
/**
Logic:
- Segment graphemes to match how terminals render clusters.
- Width rules:
1. Skip non-printing clusters (Default_Ignorable, Control, pure nonspacing/enclosing Mark, lone Surrogates). Tabs are ignored by design.
2. RGI emoji clusters (\p{RGI_Emoji}) are double-width.
3. Minimally-qualified/unqualified emoji clusters (ZWJ sequences with 2+ Extended_Pictographic, or keycap sequences) are double-width.
4. Hangul jamo collapse each standard modern Hangul L+V or L+V+T syllable piece to width 2.
Unmatched repeated leading/vowel/trailing jamo stay additive because that matches how the terminals we target render them.
5. Otherwise use East Asian Width of the cluster's first visible code point, and add widths for trailing spacing marks and Halfwidth/Fullwidth Forms within the same cluster (e.g., dakuten/handakuten/prolonged sound mark).
*/
const segmenter = new Intl.Segmenter();
const visibleCharacterRegex = /[^\p{Default_Ignorable_Code_Point}\p{Control}\p{Format}\p{Nonspacing_Mark}\p{Enclosing_Mark}\p{Surrogate}]/v;
const spacingMarkRegex = /\p{Spacing_Mark}/v;
const rgiEmojiRegex = /^\p{RGI_Emoji}$/v;
const unqualifiedKeycapRegex = /^[\d#*]\u20E3$/;
const extendedPictographicRegex = /\p{Extended_Pictographic}/gu;
function isDoubleWidthNonRgiEmojiSequence(e) {
if (e.length > 50) return false;
if (unqualifiedKeycapRegex.test(e)) return true;
if (e.includes("")) {
const t = e.match(extendedPictographicRegex);
return t !== null && t.length >= 2;
}
return false;
}
function baseVisible(e) {
const t = e.search(visibleCharacterRegex);
return t === -1 ? void 0 : e.slice(t);
}
function isHangulLeadingJamo(e) {
return e >= 4352 && e <= 4447 || e >= 43360 && e <= 43388;
}
function isHangulVowelJamo(e) {
return e >= 4448 && e <= 4519 || e >= 55216 && e <= 55238;
}
function isHangulTrailingJamo(e) {
return e >= 4520 && e <= 4607 || e >= 55243 && e <= 55291;
}
function isHangulJamo(e) {
return isHangulLeadingJamo(e) || isHangulVowelJamo(e) || isHangulTrailingJamo(e);
}
function hangulClusterWidth(e, t) {
const n = [];
for (const t of e) {
if (!visibleCharacterRegex.test(t)) continue;
n.push(t.codePointAt(0));
}
if (n.length === 0) return;
let r = 0;
for (let e = 0; e < n.length; e++) {
const i = n[e];
if (!isHangulJamo(i)) {
if (r === 0) return;
for (let i = e; i < n.length; i++) r += eastAsianWidth(n[i], t);
return r;
}
if (isHangulLeadingJamo(i) && isHangulVowelJamo(n[e + 1])) {
r += 2;
e += isHangulTrailingJamo(n[e + 2]) ? 2 : 1;
continue;
}
r += eastAsianWidth(i, t);
}
return r;
}
function trailingWidth(e, t) {
let n = 0;
let r = true;
for (const i of e) {
if (r) {
r = false;
continue;
}
if (spacingMarkRegex.test(i) || i >= "" && i <= "") n += eastAsianWidth(i.codePointAt(0), t);
}
return n;
}
function stringWidth(e, t = {}) {
if (typeof e !== "string" || e.length === 0) return 0;
const { ambiguousIsNarrow: n = true, countAnsiEscapeCodes: r = false } = t;
let i = e;
if (!r && (i.includes("\x1B") || i.includes(""))) i = stripAnsi$1(i);
if (i.length === 0) return 0;
if (/^[\u0020-\u007E]*$/.test(i)) return i.length;
let a = 0;
const o = { ambiguousAsWide: !n };
for (const { segment: e } of segmenter.segment(i)) {
const t = baseVisible(e);
if (t === void 0) continue;
if (rgiEmojiRegex.test(e) || isDoubleWidthNonRgiEmojiSequence(e)) {
a += 2;
continue;
}
const n = hangulClusterWidth(t, o);
if (n !== void 0) {
a += n;
continue;
}
const r = t.codePointAt(0);
a += eastAsianWidth(r, o);
a += trailingWidth(t, o);
}
return a;
}
//#endregion
//#region packages/terminal-core/src/ansi-sequences.ts
const ANSI_OSC_INTRODUCER_PATTERN = "(?:\\x1b\\]|\\x9d)";
const ANSI_STRING_TERMINATOR_PATTERN = "(?:\\x1b\\\\|\\x07|\\x9c)";
const ANSI_OSC_PATTERN = `${ANSI_OSC_INTRODUCER_PATTERN}[^\\x07\\x1b\\x9c]*${ANSI_STRING_TERMINATOR_PATTERN}`;
const ANSI_COMPAT_CONTROL_SEQUENCE_PATTERN = "[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]";
const ansiOscAtIndexRegex = new RegExp(ANSI_OSC_PATTERN, "y");
function matchAnsiOscAt(e, t) {
ansiOscAtIndexRegex.lastIndex = t;
return ansiOscAtIndexRegex.exec(e)?.[0];
}
function isCompatPrefixCode(e) {
return e === 91 || e === 93 || e === 40 || e === 41 || e === 35 || e === 59 || e === 63;
}
function isCompatParameterCode(e) {
return e >= 48 && e <= 57 || e === 58 || e === 59;
}
function isDigitCode(e) {
return e >= 48 && e <= 57;
}
function isCompatFinalCode(e) {
return e >= 48 && e <= 57 || e >= 64 && e <= 90 || e === 99 || e >= 102 && e <= 110 || e >= 113 && e <= 117 || e === 121 || e === 61 || e === 62 || e === 60 || e === 126;
}
/**
* Incrementally strip the ANSI grammar accepted by the agent output sanitizer.
* Parser state stays constant-size so unterminated OSC payloads cannot escape
* or accumulate outside the caller's output limits.
*/
var AnsiSequenceStripper = class {
constructor(e) {
this.onCsi = e;
this.state = "text";
this.csiCompatPrefixOnly = false;
}
startCsi() {
this.state = "csi";
this.csiCompatPrefixOnly = true;
this.csi = this.onCsi ? "" : void 0;
}
write(e) {
if (typeof e !== "string") throw new TypeError(`Expected a \`string\`, got \`${typeof e}\``);
if (this.state === "text" && !e.includes("\x1B") && !e.includes("") && !e.includes("")) return e;
const t = [];
let n = 0;
while (n < e.length) {
const r = e.charCodeAt(n);
if (this.state === "osc" || this.state === "osc-escape") {
if (r === 7 || r === 156 || this.state === "osc-escape" && r === 92) this.state = "text";
else this.state = r === 27 ? "osc-escape" : "osc";
n += 1;
continue;
}
if (r === 27 || r === 155 || r === 157) {
if (r === 155) this.startCsi();
else this.state = r === 27 ? "escape" : "osc";
n += 1;
continue;
}
if (this.state === "text") {
t.push(e.charAt(n));
n += 1;
continue;
}
if (this.state === "csi") {
if (r === 24 || r === 26) this.state = "text";
else if (r <= 31 || r === 127) t.push(e.charAt(n));
else if (r >= 32 && r <= 63) {
if (this.csi !== void 0) this.csi = this.csi.length < 64 ? this.csi + e.charAt(n) : void 0;
if (!isCompatPrefixCode(r)) this.csiCompatPrefixOnly = false;
} else if ((r === 91 || r === 93) && this.csiCompatPrefixOnly) {
this.state = "compat";
this.compatParameterDigits = void 0;
} else if (r >= 64 && r <= 126) {
if (this.csi !== void 0) this.onCsi?.(this.csi + e.charAt(n));
this.state = "text";
} else {
this.state = "text";
continue;
}
} else if (this.state === "escape") {
if (r === 93) this.state = "osc";
else if (r === 91) this.startCsi();
else if (isCompatPrefixCode(r)) {
this.state = "compat";
this.compatParameterDigits = void 0;
} else if (isDigitCode(r)) {
this.state = "compat";
this.compatParameterDigits = 1;
} else if (isCompatFinalCode(r)) this.state = "text";
else {
this.state = "text";
continue;
}
} else if (r === 24 || r === 26) this.state = "text";
else if (this.compatParameterDigits === void 0 && isCompatPrefixCode(r)) {
n += 1;
continue;
} else if (this.compatParameterDigits === void 0 && isDigitCode(r)) this.compatParameterDigits = 1;
else if (this.compatParameterDigits !== void 0 && isCompatParameterCode(r)) {
if (r === 58 || r === 59) this.compatParameterDigits = 0;
else if (this.compatParameterDigits < 4) this.compatParameterDigits += 1;
else this.state = "text";
} else if (isCompatFinalCode(r)) this.state = "text";
else {
this.state = "text";
continue;
}
n += 1;
}
return t.join("");
}
finish() {
this.csi = void 0;
this.state = "text";
this.csiCompatPrefixOnly = false;
this.compatParameterDigits = void 0;
return "";
}
};
/** Scan one CSI parser pass, retaining independently executed C0 controls. */
function scanAnsiCsiAt(e, t) {
const n = e.charCodeAt(t);
if (n !== 155 && (n !== 27 || e.charCodeAt(t + 1) !== 91)) return;
let r = t + (n === 155 ? 1 : 2);
const i = [];
let a = false;
while (r < e.length) {
const t = e.charCodeAt(r);
if (t === 24 || t === 26) {
r += 1;
a = true;
break;
}
if (t === 27 || t === 155) {
a = true;
break;
}
if (t <= 31 || t === 127) {
i.push(e.charAt(r));
r += 1;
continue;
}
if (t >= 32 && t <= 63) {
r += 1;
continue;
}
if (t >= 64 && t <= 126) r += 1;
a = true;
break;
}
return {
controls: i,
ended: a,
value: e.slice(t, r)
};
}
function* iterateAnsiSegments(e) {
let t = 0;
let n = 0;
while (n < e.length) {
const r = e.charCodeAt(n);
if (r !== 27 && r !== 155 && r !== 157) {
n += 1;
continue;
}
const i = matchAnsiOscAt(e, n);
const a = i ? void 0 : scanAnsiCsiAt(e, n);
const o = i ?? a?.value;
if (!o) {
n += 1;
continue;
}
if (n > t) yield {
kind: "text",
value: e.slice(t, n)
};
yield {
controls: a?.controls ?? [],
kind: "ansi",
value: o
};
n += o.length;
t = n;
}
if (t < e.length) yield {
kind: "text",
value: e.slice(t)
};
}
//#endregion
//#region packages/terminal-core/src/ansi.ts
const ANSI_OSC_SEQUENCE_PATTERN = `${ANSI_OSC_INTRODUCER_PATTERN}[\\s\\S]*?${ANSI_STRING_TERMINATOR_PATTERN}`;
const ANSI_COMPAT_SEQUENCE_AT_INDEX_REGEX = new RegExp(`${ANSI_OSC_SEQUENCE_PATTERN}|${ANSI_COMPAT_CONTROL_SEQUENCE_PATTERN}`, "y");
const graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
function hasAnsiIntroducer(e) {
return e.includes("\x1B") || e.includes("") || e.includes("");
}
/**
* Strip ANSI against original input positions so one removal cannot synthesize
* a second sequence. C0 controls execute without ending CSI, CAN/SUB cancel it,
* and ESC restarts escape parsing.
*/
function stripAnsiInternal(e, t) {
if (!hasAnsiIntroducer(e)) return e;
const n = [];
let r = 0;
let i = 0;
while (i < e.length) {
const a = e.charCodeAt(i);
if (a !== 27 && a !== 155 && a !== 157) {
i += 1;
continue;
}
const o = matchAnsiOscAt(e, i);
const s = o ? void 0 : scanAnsiCsiAt(e, i);
ANSI_COMPAT_SEQUENCE_AT_INDEX_REGEX.lastIndex = i;
const c = !o && t.compatibilityGrammar ? ANSI_COMPAT_SEQUENCE_AT_INDEX_REGEX.exec(e)?.[0] : void 0;
let l = o?.length ?? s?.value.length ?? c?.length;
if (l === void 0) {
i += 1;
continue;
}
if (s && !s.ended && t.preserveIncompleteCsi) break;
if (s?.controls.length === 0 && c) l = Math.max(l, c.length);
n.push(e.slice(r, i), ...s?.controls ?? []);
i += l;
r = i;
}
n.push(e.slice(r));
return n.join("");
}
function stripAnsi(e) {
return stripAnsiInternal(e, { compatibilityGrammar: false });
}
function stripAnsiSequences(e) {
if (typeof e !== "string") throw new TypeError(`Expected a \`string\`, got \`${typeof e}\``);
return stripAnsiInternal(e, { compatibilityGrammar: true });
}
/** Preserve pending CSI visibly because an output chunk boundary is not true EOF. */
function stripAnsiForStreamChunk(e, t) {
if (!hasAnsiIntroducer(e)) return e;
return stripAnsiInternal(e, {
compatibilityGrammar: t?.compatibilityGrammar === true,
preserveIncompleteCsi: true
});
}
/** Let sequential renderers consume graphemes without retaining a full-run array. */
function* iterateGraphemes(e) {
for (const { segment: t } of graphemeSegmenter.segment(e)) yield t;
}
function splitGraphemes(e) {
return Array.from(graphemeSegmenter.segment(e), (e) => e.segment);
}
const LOG_CONTROL_CHARS_REGEX = new RegExp(`[${String.fromCharCode(0)}-${String.fromCharCode(31)}${String.fromCharCode(127)}-${String.fromCharCode(159)}]`, "g");
/**
* Sanitize a value for safe interpolation into log messages.
* Strips ANSI escape sequences, C0/C1 control characters, and DEL to
* prevent log forging / terminal escape injection (CWE-117).
*/
function sanitizeForLog(e) {
return stripAnsi(e).replace(LOG_CONTROL_CHARS_REGEX, "");
}
function textWidth(e) {
const t = /[\u115F\u3164\uFFA0\uD800-\uDFFF]/u.test(e) ? e.replace(/[\uD800-\uDFFF]/gu, "�").replaceAll("ᅟ", "ᄀ").replaceAll("ㅤ", "ㄱ").replaceAll("ᅠ", "ハ") : e;
let n = stringWidth(t, { countAnsiEscapeCodes: true });
for (let t = e.indexOf(" "); t !== -1; t = e.indexOf(" ", t + 1)) n += 1;
return n;
}
function visibleWidth(e) {
return textWidth(stripAnsi(e));
}
/**
* Truncate to at most `maxWidth` visible columns, dropping whole grapheme
* clusters that would overflow while preserving zero-width ANSI sequences
* verbatim. Independently executed controls inside CSI count toward the budget
* while the containing sequence stays atomic. A single wide grapheme that
* cannot fit is dropped whole, so `visibleWidth(result) <= maxWidth`.
*/
function truncateToVisibleWidth(e, t) {
if (t <= 0) return "";
const n = stripAnsi(e);
const r = textWidth(n);
if (r <= t) return e;
let i = "";
let a = 0;
let o = false;
const appendVisible = (e) => {
if (o) return;
const s = t - a;
const c = e === n ? r : textWidth(e);
if (c <= s) {
i += e;
a += c;
return;
}
const l = graphemeSegmenter.segment(e);
const u = s <= c / 2;
let d;
let f = 0;
let p = 0;
let m = e.length;
let h = 0;
let g = 0;
const fits = (t) => {
if (t === e.length) return false;
if (!d || t < d.index || t >= d.index + d.segment.length) {
d = l.containing(t);
f = d.index === 0 ? 0 : u ? textWidth(e.slice(0, d.index)) : c - textWidth(e.slice(d.index));
}
if (f > s) return false;
h = d.index;
g = f;
return true;
};
let _ = Math.min(e.length - 1, Math.floor(s * e.length / c));
let v = fits(_);
const y = v;
let b = 1;
while (p < m) {
if (v) p = _;
else m = _;
if (v !== y || _ === 0 || _ === e.length) break;
_ = y ? Math.min(e.length, _ + b) : Math.max(0, _ - b);
b *= 2;
v = fits(_);
}
while (p + 1 < m) {
const e = Math.floor((p + m) / 2);
if (fits(e)) p = e;
else m = e;
}
i += e.slice(0, h);
a += g;
o = true;
};
for (const n of iterateAnsiSegments(e)) if (n.kind === "ansi") {
const e = n.controls.filter((e) => e === " ").length;
if (!o && a + e <= t) {
i += n.value;
a += e;
} else if (e > 0) {
i += n.value.replaceAll(" ", "");
o = true;
} else i += n.value;
} else appendVisible(n.value);
return i;
}
//#endregion
export { stripAnsiForStreamChunk as a, visibleWidth as c, eastAsianWidthType as d, stripAnsi as i, AnsiSequenceStripper as l, sanitizeForLog as n, stripAnsiSequences as o, splitGraphemes as r, truncateToVisibleWidth as s, iterateGraphemes as t, iterateAnsiSegments as u };