rojects/source/server/rojos/tools/ListFilesTool.ts

38 lines
1.0 KiB
TypeScript
Raw Normal View History

2026-07-30 06:44:46 +00:00
import fs from "fs";
import { RojoTool, RojoToolContext } from "./RojoTool.js";
import { resolveProjectPath } from "../../storage.js";
export class ListFilesTool extends RojoTool
{
readonly Definition =
{
type: "function",
function:
{
name: "list_files",
description: "List the files and subdirectories inside a directory.",
parameters:
{
type: "object",
properties:
{
path: { type: "string", description: "Path relative to the project root." },
},
required: [ "path" ],
},
},
};
async execute( args: Record<string, unknown>, ctx: RojoToolContext ): Promise<unknown>
{
const full = resolveProjectPath( ctx.projectId, args.path as string );
if ( !full ) return { error: "Invalid path" };
if ( !fs.existsSync( full ) ) return { error: "Directory does not exist" };
const entries = fs.readdirSync( full, { withFileTypes: true } );
return entries.map( e => ( { name: e.name, type: e.isDirectory() ? "directory" : "file" } ) );
}
}