@plastichub/osr-ai-tools
Version:
CLI and library for LLM tools
354 lines (353 loc) • 15 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tools = void 0;
const path = __importStar(require("path"));
const remove_1 = require("@plastichub/fs/remove");
const _glob_1 = require("@plastichub/osr-commons/_glob");
const primitives_1 = require("@plastichub/core/primitives");
const write_1 = require("@plastichub/fs/write");
const read_1 = require("@plastichub/fs/read");
const rename_1 = require("@plastichub/fs/rename");
const exists_1 = require("@plastichub/fs/exists");
const _glob_2 = require("@plastichub/osr-commons/_glob");
const __1 = require("../..");
const tools = (target, options) => {
const logger = (0, __1.toolLogger)('fs', options);
const category = 'fs';
return [
{
type: 'function',
function: {
name: 'list_files',
description: 'List all files in a directory',
parameters: {
type: 'object',
properties: {
directory: { type: 'string' },
pattern: { type: 'string', optional: true }
},
required: ['directory']
},
function: async (params) => {
try {
const directory = path.join(target, params.directory);
if (!(0, exists_1.sync)(directory)) {
logger.debug(`Tool::ListFiles Directory ${directory} does not exist`);
return [];
}
const pattern = params.pattern || '**/*';
logger.debug(`Tool::ListFiles Listing files in ${directory} with pattern ${pattern}`);
return (0, _glob_1.filesEx)(directory, [
pattern
]);
}
catch (error) {
logger.error('Error listing files', error);
throw error;
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: 'read_files',
description: 'Reads files in a directory with a given pattern',
parameters: {
type: 'object',
properties: {
directory: { type: 'string' },
pattern: { type: 'string', optional: true }
},
required: ['directory']
},
function: async (params) => {
try {
const pattern = params.pattern || '**/*';
let entries = (0, _glob_2.filesEx)(target, pattern);
let ret = entries.map((entry) => {
try {
let content = (0, read_1.sync)(entry);
return {
path: path.relative(target, entry).replace(/\\/g, '/'),
content: content.toString()
};
}
catch (error) {
logger.error(`Error reading file ${entry}:`, error);
return null;
}
});
ret = ret.filter((entry) => (entry !== null && entry.content));
logger.debug(`Tool::ReadFiles Reading files in ${target} with pattern ${pattern} : ${ret.length} files`, ret.map((entry) => entry.path));
return ret;
}
catch (error) {
logger.error('Error listing files', error);
throw error;
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: 'remove_file',
description: 'Remove a file at given path',
parameters: {
type: 'object',
properties: {
path: { type: 'string' }
},
required: ['path']
},
function: async (params) => {
try {
const filePath = path.join(target, params.path);
logger.debug(`Tool::RemoveFile Removing file ${filePath}`);
(0, remove_1.sync)(filePath);
return true;
}
catch (error) {
logger.error('Error removing file', error);
throw error;
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: 'rename_file',
description: 'Rename or move a file or directory',
parameters: {
type: 'object',
properties: {
src: { type: 'string' },
dst: { type: 'string' }
},
required: ['path']
},
function: async (params) => {
try {
const src = path.join(target, params.src);
logger.debug(`Tool::Rename file ${src} to ${params.dst}`);
(0, rename_1.sync)(src, params.dst);
(0, remove_1.sync)(src);
return true;
}
catch (error) {
logger.error('Error removing file', error);
throw error;
}
},
parse: JSON.parse
}
},
{
type: 'function',
function: {
name: "modify_project_files",
description: "Modify existing project files",
parameters: {
type: "object",
properties: {
files: {
type: "array",
items: {
type: "object",
properties: {
path: { type: "string" },
content: { type: "string" }
},
required: ["path", "content"]
}
}
},
required: ["files"],
},
function: async (ret) => {
try {
if (!target) {
logger.error(`Tool::FS:modify_project_files : Root path required`);
return;
}
let { files } = ret;
if ((0, primitives_1.isString)(files)) {
try {
files = JSON.parse(files);
}
catch (error) {
logger.error(`Tool::modify_project_files : Structure Error parsing files`, error, ret);
(0, write_1.sync)(path.join(target, 'tools-output.json'), files);
return error.message;
}
}
for (const file of files) {
const filePath = path.join(target, file.path);
logger.debug(`Tool:modify_project_files writing file ${filePath}`);
await (0, write_1.sync)(filePath, file.content);
}
}
catch (error) {
logger.error(`Error creating project structure`, error);
}
},
parse: JSON.parse,
},
},
{
type: 'function',
function: {
name: "create_file",
description: "Creates a file, given a path and content",
parameters: {
type: "object",
properties: {
file: {
type: "object",
properties: {
path: { type: "string" },
content: { type: "string" }
}
}
},
required: ["file"],
},
function: async (params) => {
params = await options.collector.onToolBefore(category, 'create_file', params) || params;
debugger;
try {
if ((0, primitives_1.isString)(params)) {
try {
params = JSON.parse(params);
}
catch (error) {
logger.error(`Tool::create_file : Structure Error parsing files`, error, params);
return error.message;
}
}
const { file } = params;
if (!target || !file.path || !file.content) {
logger.error(`Tool::create_file : Path and content are required to create file`, params);
return;
}
logger.debug(`Tool::create_file Writing file ${file.path} in ${target}`);
const filePath = path.join(target, file.path);
(0, write_1.sync)(filePath, file.content);
return true;
}
catch (error) {
logger.error(`Tool:create_file Error writing file`, error);
return false;
}
},
parse: JSON.parse,
},
},
{
type: 'function',
function: {
name: "file_exists",
description: "check if a file or folder exists",
parameters: {
type: "object",
properties: {
file: {
type: "object",
properties: {
path: { type: "string" }
}
}
},
required: ["file"],
},
function: async (ret) => {
try {
if ((0, primitives_1.isString)(ret)) {
try {
ret = JSON.parse(ret);
}
catch (error) {
logger.error(`Tool::file_exists : Structure Error parsing files`, error, ret);
return error.message;
}
}
const { file } = ret;
if (!target || !file.path) {
logger.error(`Tool::file_exists : Path is required to `, ret);
return;
}
const filePath = path.join(target, file.path);
const res = (0, exists_1.sync)(filePath);
logger.debug(`Tool::file_exists ${filePath} exists: ${res}`);
return res ? true : false;
}
catch (error) {
logger.error(`Tool:file_exists error`, error);
return false;
}
},
parse: JSON.parse,
},
},
{
type: 'function',
function: {
name: "read_file",
description: "read a file, at given a path",
parameters: {
type: "object",
properties: {
file: {
type: "object",
properties: {
path: { type: "string" }
}
}
},
required: ["file"],
},
function: async (ret) => {
try {
const { file } = ret;
const filePath = path.join(target, file.path);
logger.debug(`Tool::ReadFile Reading file ${filePath}`);
return (0, read_1.sync)(filePath, 'string');
}
catch (error) {
logger.error(`Error reading file`, error);
}
},
parse: JSON.parse
}
}
];
};
exports.tools = tools;
;