43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
import fs from 'fs';
|
|
import { defineTool, ToolError } from 'llmorbiter';
|
|
import { resolveSafe } from '../sandbox.js';
|
|
import { pushUndo } from '../undo.js';
|
|
import { emitToolCall, clip } from './notify.js';
|
|
|
|
export const mkdirTool = defineTool({
|
|
name: 'mkdir',
|
|
description: 'Make a dir in ws (recursive if parents missing).',
|
|
parameters: {
|
|
type: 'object',
|
|
additionalProperties: false,
|
|
required: ['dirname'],
|
|
properties: {
|
|
dirname: {
|
|
type: 'string',
|
|
description: 'Dir path rel to root.',
|
|
},
|
|
},
|
|
},
|
|
invoke: async ({ dirname }) => {
|
|
const absDir = resolveSafe(dirname);
|
|
if (fs.existsSync(absDir)) {
|
|
if (!fs.statSync(absDir).isDirectory()) {
|
|
throw new ToolError(`Path "${dirname}" already exists and is not a directory.`);
|
|
}
|
|
return { message: `Directory already exists: "${dirname}"`, created: false };
|
|
}
|
|
fs.mkdirSync(absDir, { recursive: true });
|
|
pushUndo({ op: 'DELETE_DIR', path: dirname });
|
|
return { message: `Directory created: "${dirname}"`, created: true };
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Call notification: the directory to create.
|
|
*/
|
|
export function notifyCall(args = {}) {
|
|
emitToolCall(mkdirTool.name, clip(JSON.stringify(args.dirname ?? '')));
|
|
}
|