ggai
Version:
OpenAI LLM Agent Interface
109 lines (106 loc) • 2.47 kB
JavaScript
const { exec } = require('child_process');
const fs = require('fs');
/**
* The functions configuration object.
*/
const config = [
{
type: 'function',
function: {
name: "writeFile",
description: "Writes content to a specified path.",
parameters: {
type: 'object',
properties: {
path: {
type: 'string',
description: 'The path to write to'
},
content: {
type: 'string',
description: 'The content to write'
}
},
required: ['path', 'content']
}
}
},
{
type: 'function',
function: {
name: "readFile",
description: "Reads content from a specified path.",
parameters: {
type: 'object',
properties: {
path: {
type: 'string',
description: 'The path to read from'
}
},
required: ['path']
}
}
},
{
type: 'function',
function: {
name: "executeCommand",
description: "Executes a bash command.",
parameters: {
type: 'object',
properties: {
command: {
type: 'string',
description: 'The command to execute'
}
},
required: ['command']
}
}
}
]
/**
* The tools object containing utility functions.
*/
const tools = {
writeFile: ({ path, content }) => {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, (err) => {
if (err) {
resolve({ error: `Error writing to file: ${err.message}` });
} else {
resolve({ result: 'File written successfully.' });
}
});
});
},
readFile: ({ path }) => {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if (err) {
resolve({ error: `Error reading from file: ${err.message}` });
} else {
resolve({ result: data });
}
});
});
},
executeCommand: ({ command }) => {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
resolve({ error: `Error executing command: ${error.message}` });
} else if (stderr) {
resolve({ error: `Command stderr: ${stderr}` });
} else {
resolve({ result: stdout });
}
});
});
}
};
module.exports = {
config,
tools
}