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

110 lines (105 loc) 4.33 kB
"use strict"; 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.wtfCommand = void 0; const axios_1 = __importDefault(require("axios")); const prompts_1 = require("@clack/prompts"); const actions_1 = require("../helpers/actions"); const conf_1 = require("../utils/conf"); const constants_1 = require("../constants"); function wtfCommand() { var _a; return __awaiter(this, void 0, void 0, function* () { const config = (0, conf_1.readConfig)(); const shellHistory = yield (0, actions_1.run_shell)({ command: ` shell=$(echo $SHELL) # Determine the history file based on the shell if [[ "$shell" == */bash ]]; then history_file=~/.bash_history elif [[ "$shell" == */zsh ]]; then history_file=~/.zsh_history else echo "Unsupported shell: $shell" exit 1 fi # Get the last 5 commands from the history file if [[ -f "$history_file" ]]; then echo "Last 5 commands:" tac "$history_file" | sed 's/^: [0-9]*:[0-9]*;//' | awk '!seen[$0]++' | head -n 5 else echo "History file not found: $history_file" fi `, shell: true, }); const { data } = yield axios_1.default.post(`${constants_1.API_HOST}${constants_1.API_VERSION}/chat`, { messages: [ { role: 'user', content: ` You're an expert in shell commands. Something went wrong with my shell history. Can you help me? Ignore the @2501 wtf command. It's yourself, don't mention it. Response with the JSON output below : <JSON_OUTPUT> { "potential_issues": [ // an array of potential issues from the history { "command_issue": "HERE_COMMAND_WITH_ISSUE", "solution": "REPLACE_WITH_SOLUTION", "comment": "REPLACE_WITH_COMMENT" // make it short and clear } ] } </JSON_OUTPUT> Shell history below : ${shellHistory} `, }, ], }, { headers: { Authorization: `Bearer ${config === null || config === void 0 ? void 0 : config.api_key}` }, }); const JSON_REGEXP = /<JSON_OUTPUT>([\s\S]*?)<\/JSON_OUTPUT>/m; const match = (_a = data.response) === null || _a === void 0 ? void 0 : _a.toString().match(JSON_REGEXP); try { const json = JSON.parse(match[1]); try { const selected = yield (0, prompts_1.select)({ message: 'Pick and execute a solution ↴', options: json.potential_issues.map((issue) => { return { value: issue.solution, label: `\x1b[31m${issue.command_issue}\x1b[0m >> \x1b[32m${issue.solution}\x1b[0m - \x1b[90m\x1b[3m${issue.comment}\x1b[0m`, }; }), }); console.log(' '); console.log(selected); const result = yield (0, actions_1.run_shell)({ command: selected, shell: true, }); console.log(result); } catch (e) { process.exit(0); } } catch (e) { console.log('Invalid JSON', match); } }); } exports.wtfCommand = wtfCommand;