@2501-ai/cli
Version:
[](https://www.npmjs.com/package/@2501-ai/cli) [](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic
145 lines (144 loc) • 5.97 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.browse_url = exports.hasError = exports.ERROR_BOL = exports.run_shell = exports.update_file = exports.write_file = exports.read_file = exports.ERRORFILE_PATH = exports.LOGFILE_PATH = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const turndown_1 = __importDefault(require("turndown"));
const execa_1 = __importDefault(require("execa"));
const cheerio = __importStar(require("cheerio"));
const logger_1 = __importDefault(require("../utils/logger"));
const sectionUpdate_1 = require("../utils/sectionUpdate");
const ignore_1 = require("../utils/ignore");
const LOG_DIR = `/tmp/2501/logs`;
exports.LOGFILE_PATH = `${LOG_DIR}/test.log`;
exports.ERRORFILE_PATH = `${LOG_DIR}/error.log`;
function read_file(args) {
logger_1.default.debug(`Reading file at "${args.path}"`);
if (!fs_1.default.existsSync(args.path))
return null;
return fs_1.default.readFileSync(args.path, 'utf8');
}
exports.read_file = read_file;
function write_file(args) {
return __awaiter(this, void 0, void 0, function* () {
logger_1.default.debug(`Writing file at "${args.path}"`);
fs_1.default.mkdirSync(path_1.default.dirname(args.path), { recursive: true });
fs_1.default.writeFileSync(args.path, args.content);
const ignoreManager = ignore_1.IgnoreManager.getInstance();
const content = ignoreManager.isIgnored(args.path)
? ''
: `Content :
${args.content}`;
return `
File written: ${args.path}
${content}`;
});
}
exports.write_file = write_file;
function update_file({ sectionsDiff, path, }) {
logger_1.default.debug('Updating sections:', sectionsDiff);
const fileContent = fs_1.default.readFileSync(path, 'utf8');
const newContent = (0, sectionUpdate_1.modifyCodeSections)({
originalContent: fileContent,
diffSections: sectionsDiff,
});
const ignoreManager = ignore_1.IgnoreManager.getInstance();
const content = ignoreManager.isIgnored(path)
? ''
: `New file Content :
\`\`\`
${newContent}
\`\`\``;
fs_1.default.writeFileSync(path, newContent);
return `
File updated: ${path}
${content}`;
}
exports.update_file = update_file;
function run_shell(args) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let output = '';
logger_1.default.debug(` Running shell command: ${args.command}`);
try {
const { stderr, stdout } = yield (0, execa_1.default)(args.command, {
shell: (_a = args.shell) !== null && _a !== void 0 ? _a : true,
env: args.env,
preferLocal: true,
});
if (stdout)
output += stdout;
if (stderr)
output += stderr;
if (!fs_1.default.existsSync(LOG_DIR)) {
fs_1.default.mkdirSync(LOG_DIR, { recursive: true });
}
fs_1.default.writeFileSync(exports.LOGFILE_PATH, output);
return output;
}
catch (error) {
return `${exports.ERROR_BOL} I failed to run ${args.command}, please fix the situation, errors below.\n ${error.message}
\`\`\`
${error}
\`\`\``;
}
});
}
exports.run_shell = run_shell;
exports.ERROR_BOL = `ERROR :`;
const hasError = (output) => {
return output.startsWith(exports.ERROR_BOL);
};
exports.hasError = hasError;
function browse_url(args) {
return __awaiter(this, void 0, void 0, function* () {
const html = yield fetch(args.url).then((res) => res.text());
const $ = cheerio.load(html);
$('script').remove();
$('style').remove();
$('iframe, img, video, object').remove();
const text = $('body').text();
const turndownService = new turndown_1.default();
const md = turndownService.turndown(text);
return `
Result of content of page :
${md.replace(/\s+/g, '')}
`;
});
}
exports.browse_url = browse_url;