0.1.3 • Published 1 year ago
@saswatb/director-worker v0.1.3
@saswatb/director-worker
A library for creating workers which provide tools for Director.
Installation
npm install @saswatb/director-worker
If using this library in node, you will also need to install the ws
package.
npm install ws
Usage
The following example shows how to create a worker which provides a tool to list files in a directory. You can get the API key from your project's dashboard within Director.
import { Worker } from "@saswatb/director-worker";
const worker = new Worker({
host: "director-production.up.railway.app",
apiKey: process.env.DIRECTOR_API_KEY,
});
worker.registerTool(
"browse-code",
"List files in the code directory, input should be the subfolder to list (can be empty)",
async (input) => {
// Importing path and fs modules using ES6 syntax
// Joining path of directory
const directoryPath = path.join(env.CODE_DIRECTORY, input);
console.log("browse-code directory path", directoryPath);
try {
// Reading directory and getting files
const files = await fs.readdir(directoryPath, { withFileTypes: true });
return files
.map((f) =>
JSON.stringify({ name: f.name, isDirectory: f.isDirectory() })
)
.join("\n");
} catch (err) {
// Handling error
console.log("Unable to scan directory: ", err);
return "Unable to scan directory: " + err;
}
}
);
worker.start();
Community
Join the Discord to chat with the community and the team.