@nanocollective/nanocoder
Version:
A local-first CLI coding agent that brings the power of agentic coding tools like Claude Code and Gemini CLI to local models or controlled APIs like OpenRouter
76 lines • 3.3 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { existsSync } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { resolve } from 'node:path';
import { Box, Text } from 'ink';
import React from 'react';
import ToolMessage from '../../components/tool-message.js';
import { ThemeContext } from '../../hooks/useTheme.js';
import { jsonSchema, tool } from '../../types/core.js';
import { isValidFilePath, resolveFilePath } from '../../utils/path-validation.js';
const executeCreateDirectory = async (args) => {
const absPath = resolve(args.path);
const alreadyExists = existsSync(absPath);
await mkdir(absPath, { recursive: true });
if (alreadyExists) {
return `Directory already exists: ${args.path}`;
}
return `Directory created: ${args.path}`;
};
const createDirectoryCoreTool = tool({
description: 'Create a directory, including parent directories if needed. Idempotent — succeeds if the directory already exists. AUTO-ACCEPTED (no user approval needed).',
inputSchema: jsonSchema({
type: 'object',
properties: {
path: {
type: 'string',
description: 'The relative path of the directory to create (e.g., "src/components/new-feature").',
},
},
required: ['path'],
}),
// Low risk: creating directories is non-destructive and idempotent
needsApproval: false,
execute: async (args, _options) => {
return await executeCreateDirectory(args);
},
});
const CreateDirectoryFormatter = React.memo(({ args, result }) => {
const themeContext = React.useContext(ThemeContext);
if (!themeContext) {
throw new Error('ThemeContext is required');
}
const { colors } = themeContext;
const messageContent = (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: colors.tool, children: "\u2692 create_directory" }), _jsxs(Box, { children: [_jsx(Text, { color: colors.secondary, children: "Path: " }), _jsx(Text, { color: colors.text, children: args.path })] }), result && (_jsxs(Box, { children: [_jsx(Text, { color: colors.secondary, children: "Result: " }), _jsx(Text, { color: colors.text, children: result })] }))] }));
return _jsx(ToolMessage, { message: messageContent, hideBox: true });
});
const createDirectoryFormatter = (args, result) => {
return _jsx(CreateDirectoryFormatter, { args: args, result: result });
};
const createDirectoryValidator = async (args) => {
if (!isValidFilePath(args.path)) {
return {
valid: false,
error: `⚒ Invalid path: "${args.path}". Path must be relative and within the project directory.`,
};
}
try {
const cwd = process.cwd();
resolveFilePath(args.path, cwd);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
return {
valid: false,
error: `⚒ Path validation failed: ${errorMessage}`,
};
}
return { valid: true };
};
export const createDirectoryTool = {
name: 'create_directory',
tool: createDirectoryCoreTool,
formatter: createDirectoryFormatter,
validator: createDirectoryValidator,
};
//# sourceMappingURL=create-directory.js.map