UNPKG

@2501-ai/cli

Version:

[![npm version](https://img.shields.io/npm/v/@2501-ai/cli.svg)](https://www.npmjs.com/package/@2501-ai/cli) [![HumanEval Score](https://img.shields.io/badge/HumanEval-96.95%25-brightgreen.svg)](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic

242 lines (241 loc) 10.4 kB
"use strict"; 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 __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Logger_spinnerStarted, _Logger_lastUpdateTime; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTerminalWidth = void 0; const p = __importStar(require("@clack/prompts")); const axios_1 = __importDefault(require("axios")); const marked_1 = require("marked"); const terminal_kit_1 = require("terminal-kit"); const conf_1 = require("../utils/conf"); const isDebug = process.env.TFZO_DEBUG === 'true'; var Colors; (function (Colors) { Colors["RED"] = "red"; Colors["GREEN"] = "green"; Colors["YELLOW"] = "yellow"; Colors["BLUE"] = "blue"; Colors["MAGENTA"] = "magenta"; Colors["CYAN"] = "cyan"; Colors["WHITE"] = "white"; })(Colors || (Colors = {})); function getTerminalWidth() { if (process.stdout.isTTY) { return process.stdout.columns; } else { return 80; } } exports.getTerminalWidth = getTerminalWidth; const stringify = (args) => { const seen = new WeakSet(); const safeStringify = (obj) => { return JSON.stringify(obj, (key, value) => { if (key.startsWith('_')) { return undefined; } if (typeof value === 'object' && value !== null) { if (seen.has(value)) { return '[Circular]'; } seen.add(value); } return value; }, 2); }; return args.map((a) => { if (a instanceof Error) { return `${a.message}\n${a.stack}\n`; } return `${typeof a === 'object' ? safeStringify(a) : a}` + '\n'; }); }; class Logger { constructor(spin = p.spinner()) { this.spin = spin; _Logger_spinnerStarted.set(this, false); _Logger_lastUpdateTime.set(this, 0); } intro(message) { p.intro(message); } log(message) { p.log.message(message); } outro(message) { p.outro(message); } cancel(message, stopMessage) { this.stop(stopMessage); p.cancel(message); } start(message) { var _a, _b, _c, _d; if ((_a = (0, conf_1.readConfig)()) === null || _a === void 0 ? void 0 : _a.disable_spinner) { p.log.message(message); return; } const terminalWidth = getTerminalWidth(); const maxMessageLength = terminalWidth - 10; const truncatedMessage = message ? message.substring(0, maxMessageLength) : undefined; if (__classPrivateFieldGet(this, _Logger_spinnerStarted, "f")) { (_b = this.spin) === null || _b === void 0 ? void 0 : _b.message(truncatedMessage); return; } if (truncatedMessage && truncatedMessage.length > maxMessageLength) { (_c = this.spin) === null || _c === void 0 ? void 0 : _c.message(truncatedMessage.slice(0, maxMessageLength)); } (_d = this.spin) === null || _d === void 0 ? void 0 : _d.start(truncatedMessage); __classPrivateFieldSet(this, _Logger_spinnerStarted, true, "f"); } message(message) { var _a, _b; if ((_a = (0, conf_1.readConfig)()) === null || _a === void 0 ? void 0 : _a.disable_spinner) { p.log.message(message); return; } const now = Date.now(); if (now - __classPrivateFieldGet(this, _Logger_lastUpdateTime, "f") < 100) { return; } __classPrivateFieldSet(this, _Logger_lastUpdateTime, now, "f"); const terminalWidth = getTerminalWidth(); const maxMessageLength = terminalWidth - 10; const truncatedMessage = message.substring(0, maxMessageLength); (_b = this.spin) === null || _b === void 0 ? void 0 : _b.message(truncatedMessage); } stop(message, code) { var _a, _b, _c; if ((_a = (0, conf_1.readConfig)()) === null || _a === void 0 ? void 0 : _a.disable_spinner) { p.log.message(message); return; } if (!__classPrivateFieldGet(this, _Logger_spinnerStarted, "f")) { (_b = this.spin) === null || _b === void 0 ? void 0 : _b.message(marked_1.marked.parse(message || '')); return; } (_c = this.spin) === null || _c === void 0 ? void 0 : _c.stop(marked_1.marked.parse(message || '').trim(), code); __classPrivateFieldSet(this, _Logger_spinnerStarted, false, "f"); } prompt(message, promptType = 'boolean') { if (promptType === 'string') { return p.password({ message, validate: (value) => { if (!value) return 'Please enter an Api Key.'; if (!value.startsWith('2501_ak_')) return 'Please enter a valid Api Key.'; }, }); } return p.select({ message, options: [ { value: true, label: 'Yes' }, { value: false, label: 'No' }, ], initialValue: false, }); } handleError(e, defaultMsg = 'Unexpected error. Please try again !') { var _a, _b, _c, _d, _e, _f, _g, _h, _j; if (isDebug) { if (!axios_1.default.isAxiosError(e)) { Logger.error('Command error', e); return this.cancel(defaultMsg); } const axiosError = e; Logger.error('Command error - Axios error', { code: axiosError.code, message: axiosError.message, name: axiosError.name, responseData: ((_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.data) || 'no error', data: axiosError.toJSON(), }); if (axiosError.code === 'ECONNREFUSED') { defaultMsg = 'Server unreachable. Please try again later.'; } return this.cancel(((_c = (_b = axiosError.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.error) || defaultMsg); } if (!axios_1.default.isAxiosError(e)) { return this.cancel(defaultMsg); } const axiosError = e; const errorData = (_d = axiosError.response) === null || _d === void 0 ? void 0 : _d.data; if (((_e = axiosError.response) === null || _e === void 0 ? void 0 : _e.status) === 401) { defaultMsg = 'Unauthorized. Please verify your API key.'; } if (((_f = axiosError.response) === null || _f === void 0 ? void 0 : _f.status) === 403) { if ((errorData === null || errorData === void 0 ? void 0 : errorData.code) === 'TOKEN_LIMIT') { defaultMsg = 'Monthly token usage limit reached. Please upgrade your plan or contact us !'; } } if (((_g = axiosError.response) === null || _g === void 0 ? void 0 : _g.status) === 500) { defaultMsg = 'The server has returned an error. Please try again'; } if (axiosError.code === 'ECONNREFUSED') { defaultMsg = 'Server unreachable. Please try again later.'; } this.cancel(((_j = (_h = axiosError.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.error) || defaultMsg); } static agent(data) { p.outro(marked_1.marked.parse(data)); } static log(...args) { terminal_kit_1.terminal.defaultColor(...args.map((a) => (typeof a === 'object' ? JSON.stringify(a, null, 2) : a) + '\n')); } static error(...args) { terminal_kit_1.terminal[Colors.RED]('\n[ERROR] ').defaultColor(...stringify(args)); } static debug(...args) { if (isDebug) { terminal_kit_1.terminal[Colors.MAGENTA]('[DEBUG] ').defaultColor(...stringify(args)); } } static warn(...args) { terminal_kit_1.terminal[Colors.YELLOW]('[WARN] ').defaultColor(...stringify(args)); } } exports.default = Logger; _Logger_spinnerStarted = new WeakMap(), _Logger_lastUpdateTime = new WeakMap();