hataraku
Version:
An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.
105 lines • 4.64 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.listFilesTool = void 0;
const zod_1 = require("zod");
const path = __importStar(require("path"));
const list_files_1 = require("../../services/glob/list-files");
/**
* Tool for listing files and directories within a specified directory.
*
* @remarks
* This tool provides functionality to list files in a directory, with options for recursive listing.
* It returns relative paths to make the output more readable and useful.
*
* @example
* ```typescript
* // List files in the current directory (non-recursive)
* const result = await listFilesTool.execute({ path: '.' });
*
* // List all files recursively in the src directory
* const recursiveResult = await listFilesTool.execute({ path: 'src', recursive: true });
* ```
*
* @throws Will throw an error if the directory doesn't exist or cannot be accessed
*/
exports.listFilesTool = {
description: "List files and directories within the specified directory. If recursive is true, it lists all files (with relative paths) recursively.",
parameters: zod_1.z.object({
path: zod_1.z.string().describe('The path of the directory to list contents for (relative to the current working directory)'),
recursive: zod_1.z.boolean().optional().describe('Set to true for a recursive listing, false or omitted for top-level only.')
}),
/**
* Executes the list files operation on the specified directory.
*
* @param options - The options for listing files
* @param options.path - The directory path to list files from (relative to current working directory)
* @param options.recursive - Whether to list files recursively (defaults to false)
* @returns An object containing the listing results as formatted text
* @throws Will throw and return an error message if the directory cannot be accessed
*/
execute: async ({ path: dirPath, recursive = false }) => {
try {
const absolutePath = path.resolve(process.cwd(), dirPath);
const [files, hasMore] = await (0, list_files_1.listFiles)(absolutePath, recursive, 1000);
// Convert absolute paths to relative paths
const relativePaths = files.map(file => {
const relPath = path.relative(absolutePath, file);
// Remove trailing slash from directories and normalize path separators
return relPath.replace(/[\\/]+$/, '').split(path.sep).join('/');
});
const message = relativePaths.length > 0
? relativePaths.sort().join('\n')
: '(empty directory)';
return {
content: [{
type: "text",
text: hasMore ? `${message}\n(showing first 1000 entries)` : message
}]
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
isError: true,
content: [{
type: "text",
text: `Error listing files: ${errorMessage}`
}]
};
}
}
};
//# sourceMappingURL=list-files.js.map