mirror of
https://github.com/tvytlx/ai-agent-deep-dive.git
synced 2026-04-05 00:24:50 +08:00
Add extracted source directory and README navigation
This commit is contained in:
50
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/ansiCodes.js
generated
vendored
Normal file
50
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/ansiCodes.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
import ansiStyles from "ansi-styles";
|
||||
export const ESCAPES = new Set([27, 155]); // \x1b and \x9b
|
||||
export const CSI = "[".codePointAt(0);
|
||||
export const OSC = "]".codePointAt(0);
|
||||
export const endCodesSet = new Set();
|
||||
const endCodesMap = new Map();
|
||||
for (const [start, end] of ansiStyles.codes) {
|
||||
endCodesSet.add(ansiStyles.color.ansi(end));
|
||||
endCodesMap.set(ansiStyles.color.ansi(start), ansiStyles.color.ansi(end));
|
||||
}
|
||||
export const linkStartCodePrefix = "\x1B]8;;";
|
||||
export const linkStartCodePrefixCharCodes = linkStartCodePrefix
|
||||
.split("")
|
||||
.map((char) => char.charCodeAt(0));
|
||||
export const linkCodeSuffix = "\x07";
|
||||
export const linkCodeSuffixCharCode = linkCodeSuffix.charCodeAt(0);
|
||||
export const linkEndCode = `\x1B]8;;${linkCodeSuffix}`;
|
||||
export function getLinkStartCode(url) {
|
||||
return `${linkStartCodePrefix}${url}${linkCodeSuffix}`;
|
||||
}
|
||||
export function getEndCode(code) {
|
||||
if (endCodesSet.has(code))
|
||||
return code;
|
||||
if (endCodesMap.has(code))
|
||||
return endCodesMap.get(code);
|
||||
// We have a few special cases to handle here:
|
||||
// Links:
|
||||
if (code.startsWith(linkStartCodePrefix))
|
||||
return linkEndCode;
|
||||
code = code.slice(2);
|
||||
// 8-bit/24-bit colors:
|
||||
if (code.startsWith("38")) {
|
||||
return ansiStyles.color.close;
|
||||
}
|
||||
else if (code.startsWith("48")) {
|
||||
return ansiStyles.bgColor.close;
|
||||
}
|
||||
// Otherwise find the reset code in the ansi-styles map
|
||||
const ret = ansiStyles.codes.get(parseInt(code, 10));
|
||||
if (ret) {
|
||||
return ansiStyles.color.ansi(ret);
|
||||
}
|
||||
else {
|
||||
return ansiStyles.reset.open;
|
||||
}
|
||||
}
|
||||
export function ansiCodesToString(codes) {
|
||||
return codes.map((code) => code.code).join("");
|
||||
}
|
||||
//# sourceMappingURL=ansiCodes.js.map
|
||||
17
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/diff.js
generated
vendored
Normal file
17
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/diff.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import { undoAnsiCodes } from "./undo.js";
|
||||
/**
|
||||
* Returns the minimum amount of ANSI codes necessary to get from the compound style `from` to `to`.
|
||||
* Both `from` and `to` are expected to be reduced.
|
||||
*/
|
||||
export function diffAnsiCodes(from, to) {
|
||||
const endCodesInTo = new Set(to.map((code) => code.endCode));
|
||||
const startCodesInFrom = new Set(from.map((code) => code.code));
|
||||
return [
|
||||
// Ignore all styles in `from` that are not overwritten or removed by `to`
|
||||
// Disable all styles in `from` that are removed in `to`
|
||||
...undoAnsiCodes(from.filter((code) => !endCodesInTo.has(code.endCode))),
|
||||
// Add all styles in `to` that don't exist in `from`
|
||||
...to.filter((code) => !startCodesInFrom.has(code.code)),
|
||||
];
|
||||
}
|
||||
//# sourceMappingURL=diff.js.map
|
||||
7
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/index.js
generated
vendored
Normal file
7
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/index.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
export { ansiCodesToString } from "./ansiCodes.js";
|
||||
export { diffAnsiCodes } from "./diff.js";
|
||||
export { reduceAnsiCodes, reduceAnsiCodesIncremental } from "./reduce.js";
|
||||
export * from "./styledChars.js";
|
||||
export * from "./tokenize.js";
|
||||
export { undoAnsiCodes } from "./undo.js";
|
||||
//# sourceMappingURL=index.js.map
|
||||
38
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/reduce.js
generated
vendored
Normal file
38
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/reduce.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
import ansiStyles from "ansi-styles";
|
||||
import { endCodesSet } from "./ansiCodes.js";
|
||||
/** Reduces the given array of ANSI codes to the minimum necessary to render with the same style */
|
||||
export function reduceAnsiCodes(codes) {
|
||||
return reduceAnsiCodesIncremental([], codes);
|
||||
}
|
||||
/** Like {@link reduceAnsiCodes}, but assumes that `codes` is already reduced. Further reductions are only done for the items in `newCodes`. */
|
||||
export function reduceAnsiCodesIncremental(codes, newCodes) {
|
||||
let ret = [...codes];
|
||||
for (const code of newCodes) {
|
||||
if (code.code === ansiStyles.reset.open) {
|
||||
// Reset code, disable all codes
|
||||
ret = [];
|
||||
}
|
||||
else if (endCodesSet.has(code.code)) {
|
||||
// This is an end code, disable all matching start codes
|
||||
ret = ret.filter((retCode) => retCode.endCode !== code.code);
|
||||
}
|
||||
else {
|
||||
// This is a start code. Remove codes it "overrides", then add it.
|
||||
// If a new code has the same endCode, it "overrides" existing ones.
|
||||
// Special case: Intensity codes (1m, 2m) can coexist (both end with 22m).
|
||||
const isIntensityCode = code.code === ansiStyles.bold.open || code.code === ansiStyles.dim.open;
|
||||
// Add intensity codes only if not already present
|
||||
if (isIntensityCode) {
|
||||
if (!ret.find((retCode) => retCode.code === code.code && retCode.endCode === code.endCode)) {
|
||||
ret.push(code);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = ret.filter((retCode) => retCode.endCode !== code.endCode);
|
||||
ret.push(code);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
//# sourceMappingURL=reduce.js.map
|
||||
38
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/styledChars.js
generated
vendored
Normal file
38
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/styledChars.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
import { ansiCodesToString } from "./ansiCodes.js";
|
||||
import { diffAnsiCodes } from "./diff.js";
|
||||
import { reduceAnsiCodesIncremental } from "./reduce.js";
|
||||
export function styledCharsFromTokens(tokens) {
|
||||
let codes = [];
|
||||
const ret = [];
|
||||
for (const token of tokens) {
|
||||
if (token.type === "ansi") {
|
||||
codes = reduceAnsiCodesIncremental(codes, [token]);
|
||||
}
|
||||
else if (token.type === "char") {
|
||||
ret.push({
|
||||
...token,
|
||||
styles: [...codes],
|
||||
});
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
export function styledCharsToString(chars) {
|
||||
let ret = "";
|
||||
for (let i = 0; i < chars.length; i++) {
|
||||
const char = chars[i];
|
||||
if (i === 0) {
|
||||
ret += ansiCodesToString(char.styles);
|
||||
}
|
||||
else {
|
||||
ret += ansiCodesToString(diffAnsiCodes(chars[i - 1].styles, char.styles));
|
||||
}
|
||||
ret += char.value;
|
||||
// reset active styles at the end of the string
|
||||
if (i === chars.length - 1) {
|
||||
ret += ansiCodesToString(diffAnsiCodes(char.styles, []));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
//# sourceMappingURL=styledChars.js.map
|
||||
141
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/tokenize.js
generated
vendored
Normal file
141
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/tokenize.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
import isFullwidthCodePoint from "is-fullwidth-code-point";
|
||||
import { CSI, ESCAPES, getEndCode, linkStartCodePrefix, linkStartCodePrefixCharCodes, OSC, } from "./ansiCodes.js";
|
||||
// HOT PATH: Use only basic string/char code operations for maximum performance
|
||||
function parseLinkCode(string, offset) {
|
||||
string = string.slice(offset);
|
||||
for (let index = 1; index < linkStartCodePrefixCharCodes.length; index++) {
|
||||
if (string.charCodeAt(index) !== linkStartCodePrefixCharCodes[index]) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
// This is a link code (with or without the URL part). Find the end of it.
|
||||
const endIndex = string.indexOf("\x07", linkStartCodePrefix.length);
|
||||
if (endIndex === -1)
|
||||
return undefined;
|
||||
return string.slice(0, endIndex + 1);
|
||||
}
|
||||
const CC_0 = "0".charCodeAt(0);
|
||||
const CC_9 = "9".charCodeAt(0);
|
||||
const CC_SEMI = ";".charCodeAt(0);
|
||||
const CC_M = "m".charCodeAt(0);
|
||||
/**
|
||||
* Scans through the given string and finds the index of the last character of an SGR sequence
|
||||
* like `\x1B[38;2;123;123;123m`. This assumes that the string has been checked to start with `\x1B[`.
|
||||
* Returns -1 if no valid SGR sequence is found.
|
||||
*/
|
||||
function findSGRSequenceEndIndex(str) {
|
||||
for (let index = 2; index < str.length; index++) {
|
||||
const charCode = str.charCodeAt(index);
|
||||
// m marks the end of the SGR sequence
|
||||
if (charCode === CC_M)
|
||||
return index;
|
||||
// Digits and semicolons are valid
|
||||
if (charCode === CC_SEMI)
|
||||
continue;
|
||||
if (charCode >= CC_0 && charCode <= CC_9)
|
||||
continue;
|
||||
// Everything else is invalid
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
// HOT PATH: Use only basic string/char code operations for maximum performance
|
||||
function parseSGRSequence(string, offset) {
|
||||
string = string.slice(offset);
|
||||
const endIndex = findSGRSequenceEndIndex(string);
|
||||
if (endIndex === -1)
|
||||
return;
|
||||
return string.slice(0, endIndex + 1);
|
||||
}
|
||||
/**
|
||||
* Splits compound SGR sequences like `\x1B[1;3;31m` into individual components
|
||||
*/
|
||||
function splitCompoundSGRSequences(code) {
|
||||
if (!code.includes(";")) {
|
||||
// Not a compound code
|
||||
return [code];
|
||||
}
|
||||
const codeParts = code
|
||||
// Strip off the escape sequences \x1B[ and m
|
||||
.slice(2, -1)
|
||||
.split(";");
|
||||
const ret = [];
|
||||
for (let i = 0; i < codeParts.length; i++) {
|
||||
const rawCode = codeParts[i];
|
||||
// Keep 8-bit and 24-bit color codes (containing multiple ";") together
|
||||
if (rawCode === "38" || rawCode === "48") {
|
||||
if (i + 2 < codeParts.length && codeParts[i + 1] === "5") {
|
||||
// 8-bit color, followed by another number
|
||||
ret.push(codeParts.slice(i, i + 3).join(";"));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
else if (i + 4 < codeParts.length && codeParts[i + 1] === "2") {
|
||||
// 24-bit color, followed by three numbers
|
||||
ret.push(codeParts.slice(i, i + 5).join(";"));
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Not a (valid) 8/24-bit color code, push as is
|
||||
ret.push(rawCode);
|
||||
}
|
||||
return ret.map((part) => `\x1b[${part}m`);
|
||||
}
|
||||
export function tokenize(str, endChar = Number.POSITIVE_INFINITY) {
|
||||
const ret = [];
|
||||
let index = 0;
|
||||
let visible = 0;
|
||||
while (index < str.length) {
|
||||
const codePoint = str.codePointAt(index);
|
||||
if (ESCAPES.has(codePoint)) {
|
||||
let code;
|
||||
// Peek the next code point to determine the type of ANSI sequence
|
||||
const nextCodePoint = str.codePointAt(index + 1);
|
||||
if (nextCodePoint === OSC) {
|
||||
// ] = operating system commands, like links
|
||||
code = parseLinkCode(str, index);
|
||||
if (code) {
|
||||
ret.push({
|
||||
type: "ansi",
|
||||
code: code,
|
||||
endCode: getEndCode(code),
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (nextCodePoint === CSI) {
|
||||
// [ = control sequence introducer, like SGR sequences [...m
|
||||
code = parseSGRSequence(str, index);
|
||||
if (code) {
|
||||
// Split compound codes into individual tokens
|
||||
const codes = splitCompoundSGRSequences(code);
|
||||
for (const individualCode of codes) {
|
||||
ret.push({
|
||||
type: "ansi",
|
||||
code: individualCode,
|
||||
endCode: getEndCode(individualCode),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (code) {
|
||||
index += code.length;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const fullWidth = isFullwidthCodePoint(codePoint);
|
||||
const character = String.fromCodePoint(codePoint);
|
||||
ret.push({
|
||||
type: "char",
|
||||
value: character,
|
||||
fullWidth,
|
||||
});
|
||||
index += character.length;
|
||||
visible += fullWidth ? 2 : character.length;
|
||||
if (visible >= endChar) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
//# sourceMappingURL=tokenize.js.map
|
||||
11
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/undo.js
generated
vendored
Normal file
11
extracted-source/node_modules/@alcalzone/ansi-tokenize/build/undo.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { reduceAnsiCodes } from "./reduce.js";
|
||||
/** Returns the combination of ANSI codes needed to undo the given ANSI codes */
|
||||
export function undoAnsiCodes(codes) {
|
||||
return reduceAnsiCodes(codes)
|
||||
.reverse()
|
||||
.map((code) => ({
|
||||
...code,
|
||||
code: code.endCode,
|
||||
}));
|
||||
}
|
||||
//# sourceMappingURL=undo.js.map
|
||||
Reference in New Issue
Block a user