Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 7x 7x 7x 7x 7x 7x 7x 7x 13x 13x 13x 2x 2x 2x 2x 6x 6x 1x 5x 5x 1x 4x 4x 4x 1x 1x 7x 7x 7x 1x 1x 1x 6x 6x 4x 4x 4x 1x 3x 2x 1x 1x | import {
BadRequestException,
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import * as fs from 'fs/promises';
import * as path from 'path';
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import { ActionHandler } from './action-handler.interface';
import {
ActionExecutionResult,
PlanExecutionContext,
ToolMetadata,
} from '../llm-orchestration.interfaces';
import { DeleteFileArgsDto } from './dto/delete-file.args.dto';
import { generateToolCall, generateToolCallJson } from '../../utils';
@Injectable()
export class DeleteFileHandler implements ActionHandler {
readonly toolName = 'delete_file';
private readonly logger = new Logger(DeleteFileHandler.name);
private readonly projectRoot: string =
process.env.REPOBURG_PROJECT_PATH || process.cwd();
getMetadata(): ToolMetadata {
return {
name: this.toolName,
description: this.getDefinition(true),
arguments: [
{
name: 'file_path',
type: 'string',
description: 'The relative path of file to delete.',
required: true,
},
],
};
}
/**
* Generates a tool call example in the specified format.
* @param toolCall - The tool call object to format
* @param useJson - If true, uses JSON format; otherwise uses XML-style format
* @returns Formatted tool call string
*/
private generateExample(
toolCall: Record<string, any>,
useJson: boolean = false,
): string {
return useJson
? generateToolCallJson(toolCall)
: generateToolCall(toolCall);
}
getDefinition(useJsonFormat: boolean = false): string {
const example = this.generateExample(
{
tool_name: this.toolName,
file_path: 'src/legacy/old-api-client.js',
},
useJsonFormat,
);
const definition = `
-------------
### ${this.toolName}
Deletes a file from the filesystem.
#### Parameters
- "file_path": (string) The relative path of file to delete.
#### Example
Explanation: Remove the old, unused 'legacy.js' file.
:${example}
-------------
`;
return `\n${definition.trim()}\n`;
}
private resolveAndValidatePath(unsafePath: string): string {
const normalizedPath = path.normalize(unsafePath);
if (path.isAbsolute(normalizedPath)) {
throw new BadRequestException(
`Absolute paths are not allowed: ${unsafePath}`,
);
}
const resolvedPath = path.resolve(this.projectRoot, normalizedPath);
if (!resolvedPath.startsWith(this.projectRoot)) {
throw new BadRequestException(
`Path traversal is not allowed: ${unsafePath}`,
);
}
return resolvedPath;
}
private async readFileContent(filePath: string): Promise<string | null> {
try {
return await fs.readFile(filePath, 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
return null;
}
throw error;
}
}
async execute(
args: { [key: string]: any },
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_context: PlanExecutionContext,
): Promise<ActionExecutionResult> {
const validatedArgs = plainToClass(DeleteFileArgsDto, args);
const errors = await validate(validatedArgs);
if (errors.length > 0) {
const errorMessages = errors
.map((err) => Object.values(err.constraints || {}).join(', '))
.join('; ');
return {
status: 'FAILURE',
summary: `Invalid arguments for ${this.toolName}.`,
error_message: errorMessages,
persisted_args: args,
execution_log: { output: '', error_message: errorMessages },
};
}
const { file_path } = validatedArgs;
const safePath = this.resolveAndValidatePath(file_path);
const originalContent = await this.readFileContent(safePath);
try {
await fs.unlink(safePath);
return {
status: 'SUCCESS',
summary: `File "${file_path}" deleted.`,
persisted_args: { file_path },
original_content_for_revert: originalContent,
execution_log: {
output: `${file_path} has been deleted.`,
error_message: '',
},
};
} catch (error) {
if (error.code === 'ENOENT') {
return {
status: 'SUCCESS',
summary: `File "${file_path}" did not exist, considered deleted.`,
persisted_args: { file_path },
original_content_for_revert: null,
execution_log: {
output: `${file_path} did not exist.`,
error_message: '',
},
};
}
this.logger.error(
`Failed to delete file at ${file_path}: ${error.message}`,
);
throw new InternalServerErrorException(
`Failed to delete file "${file_path}": ${error.message}`,
);
}
}
}
|