@plastichub/osr-ai-tools
Version:
CLI and library for LLM tools
161 lines (160 loc) • 6.53 kB
JavaScript
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_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const __1 = require("../..");
const p_map_1 = __importDefault(require("p-map"));
const exists_1 = require("@plastichub/fs/exists");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
const install = async (dependency, directory) => {
return new Promise((resolve, reject) => {
const command = `pnpm add ${dependency} --dir ${directory}`;
(0, child_process_1.exec)(command, (error, stdout, stderr) => {
if (error) {
__1.logger.error(`Error installing ${dependency}:`, error.message);
return resolve(false);
}
__1.logger.info(`Successfully installed "${dependency}" in "${directory}".`);
setTimeout(() => {
resolve(true);
}, 1000);
});
});
};
const __2 = require("../..");
const tools = (target, options) => {
const logger = (0, __2.toolLogger)(path_1.default.parse(__filename).name, options);
return [
{
type: 'function',
function: {
name: 'build_project',
description: 'Build project using pnpm build command',
parameters: {
type: 'object',
properties: {},
required: []
},
function: async () => {
try {
logger.debug(`Tool::BuildProject Building project at ${target}`);
const { stdout, stderr } = await execAsync('pnpm build', {
cwd: target
});
return {
success: !stderr,
output: stdout,
error: stderr || null
};
}
catch (error) {
logger.error('Error building project', error);
return {
success: false,
output: null,
error: error.message
};
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: 'run_npm',
description: 'Run an npm/pnpm command',
parameters: {
type: 'object',
properties: {
command: { type: 'string', description: 'Command to run (e.g. install, test, etc)' },
args: {
type: 'array',
items: { type: 'string' },
description: 'Additional arguments for the command',
optional: true
}
},
required: ['command']
},
function: async (params) => {
try {
const args = params.args ? params.args.join(' ') : '';
const fullCommand = `pnpm ${params.command} ${args}`.trim();
logger.debug(`Tool::RunNpm Running command: ${fullCommand}`);
const { stdout, stderr } = await execAsync(fullCommand, {
cwd: target
});
return {
success: !stderr,
output: stdout,
error: stderr || null
};
}
catch (error) {
logger.error('Error running npm command', error);
return {
success: false,
output: null,
error: error.message
};
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: "install_dependency",
description: "Install a dependency using npm",
parameters: {
type: "object",
properties: {
dependencies: {
type: "array",
items: {
type: "string"
}
}
},
required: ["dependencies"],
},
function: async (ret) => {
try {
const { dependencies } = ret;
if (!target) {
logger.error(`Tool::NPM Target is required to install dependencies`);
return;
}
if (!(0, exists_1.sync)(target)) {
logger.error(`Project doesnt path exists ${target}`);
return;
}
await (0, p_map_1.default)(dependencies, (async (dependency) => {
logger.info(`Installing dependency`, dependency);
try {
return install(dependency, target);
}
catch (error) {
logger.error(`Error installing dependency ${dependency} `, error);
}
}), {
concurrency: 1
});
}
catch (error) {
logger.error(`Error installing dependencies`, error);
}
},
parse: JSON.parse,
}
}
];
};
exports.tools = tools;
;