UNPKG

@plastichub/osr-ai-tools

Version:

CLI and library for LLM tools

186 lines (185 loc) 7.41 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tools = void 0; const path = __importStar(require("path")); const simple_git_1 = __importDefault(require("simple-git")); const exists_1 = require("@plastichub/fs/exists"); const osr_commons_1 = require("@plastichub/osr-commons"); const __1 = require("../../"); const __2 = require("../.."); const find_up_1 = require("find-up"); const commitFiles = async (filePaths, commitMessage, targetDirectory, variables = {}) => { try { if (!filePaths || !filePaths.length) { __1.logger.warn(`No files to commit`); return; } if (!(0, exists_1.sync)(path.join(targetDirectory, '.git'))) { try { __1.logger.info(`Initializing repository at ${targetDirectory}`); await initRepository(targetDirectory); } catch (e) { __1.logger.error(`Error initializing repository at ${targetDirectory} `, e.message, filePaths); } } const git = (0, simple_git_1.default)(targetDirectory); try { await git.add(filePaths); } catch (e) { __1.logger.error('Error adding files:', e.message, filePaths); } await git.commit(commitMessage); try { await git.raw(['branch', '-M', 'master']); const repo = (0, osr_commons_1.substitute)(false, "${GIT_REPO}/${GIT_USER}/${REPO_NAME}.git", { REPO_NAME: path.basename(targetDirectory), ...variables }); await git.raw(['remote', 'add', 'origin', repo]); await git.push(['--set-upstream', 'origin', 'master']); } catch (e) { if (e.message.includes('remote origin already exists')) { await git.push(['--set-upstream', 'origin', 'master']); } else { __1.logger.error('Tools::GIT : Error pushing files:', e.message, filePaths); } } __1.logger.info('Files committed successfully!', filePaths); } catch (error) { __1.logger.error('Error committing files:', error.message, filePaths); throw error; } }; const initRepository = async (targetDirectory, variables = {}) => { try { const git = (0, simple_git_1.default)(targetDirectory); if (!(0, exists_1.sync)(path.join(targetDirectory, '.git'))) { await git.init(); __1.logger.info('Git repository initialized successfully!'); return true; } __1.logger.info('Git repository already exists'); return false; } catch (error) { __1.logger.error('Error initializing git repository:', error.message); return false; } }; const tools = (target, options) => { const logger = (0, __2.toolLogger)(path.parse(__filename).name, options); if (!target) { logger.warn(`Tools:GIT : Target is required`); return []; } if (!(0, exists_1.sync)(target)) { logger.warn(`Tools:GIT : Project path doesnt exists ${target}`); return []; } return [ { type: 'function', function: { name: "init_repository", description: "Initialize a new git repository", parameters: { type: "object", properties: {}, required: [] }, function: async (params) => { logger.info(`Tool::init_repository Init Repository in ${target}`); const gitDir = (0, find_up_1.sync)('.git', { type: 'directory', cwd: target }); if (gitDir && (0, exists_1.sync)(gitDir)) { logger.info(`Repository already exists at ${gitDir}`); return true; } try { const ret = await initRepository(target, options.variables); return true; } catch (error) { logger.error(`Error initializing repository`, error); return false; } }, parse: JSON.parse, } }, { type: 'function', function: { name: "commit_files_git", description: "Commit files using git", parameters: { type: "object", properties: { files: { type: "array", items: { type: "string" } }, message: { type: "string" } }, required: ["files"], }, function: async (ret) => { debugger; try { const { files, message } = ret; logger.info(`Tool::GIT Commit files ${files} in ${target}`); if (!target) { logger.error(`Tool::Git Commit : Target is required`); return; } if (!(0, exists_1.sync)(target)) { logger.error(`Project doesnt path exists ${target}`); return; } await commitFiles(files, message, target, options.variables); } catch (error) { logger.error(`Error committing dependencies : ${error.message}`); } return true; }, parse: JSON.parse, }, } ]; }; exports.tools = tools;