mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 08:47:44 +00:00
Pure refactor: Consolidate isWithinRoot() function calling. (#4163)
This commit is contained in:
committed by
GitHub
parent
e584241141
commit
fefa7ecbea
@@ -46,10 +46,7 @@ export interface ReadFileToolParams {
|
||||
export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
|
||||
static readonly Name: string = 'read_file';
|
||||
|
||||
constructor(
|
||||
private rootDirectory: string,
|
||||
private config: Config,
|
||||
) {
|
||||
constructor(private config: Config) {
|
||||
super(
|
||||
ReadFileTool.Name,
|
||||
'ReadFile',
|
||||
@@ -76,7 +73,6 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
|
||||
type: Type.OBJECT,
|
||||
},
|
||||
);
|
||||
this.rootDirectory = path.resolve(rootDirectory);
|
||||
}
|
||||
|
||||
validateToolParams(params: ReadFileToolParams): string | null {
|
||||
@@ -89,8 +85,8 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
|
||||
if (!path.isAbsolute(filePath)) {
|
||||
return `File path must be absolute, but was relative: ${filePath}. You must provide an absolute path.`;
|
||||
}
|
||||
if (!isWithinRoot(filePath, this.rootDirectory)) {
|
||||
return `File path must be within the root directory (${this.rootDirectory}): ${filePath}`;
|
||||
if (!isWithinRoot(filePath, this.config.getTargetDir())) {
|
||||
return `File path must be within the root directory (${this.config.getTargetDir()}): ${filePath}`;
|
||||
}
|
||||
if (params.offset !== undefined && params.offset < 0) {
|
||||
return 'Offset must be a non-negative number';
|
||||
@@ -115,7 +111,10 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
|
||||
) {
|
||||
return `Path unavailable`;
|
||||
}
|
||||
const relativePath = makeRelative(params.absolute_path, this.rootDirectory);
|
||||
const relativePath = makeRelative(
|
||||
params.absolute_path,
|
||||
this.config.getTargetDir(),
|
||||
);
|
||||
return shortenPath(relativePath);
|
||||
}
|
||||
|
||||
@@ -133,7 +132,7 @@ export class ReadFileTool extends BaseTool<ReadFileToolParams, ToolResult> {
|
||||
|
||||
const result = await processSingleFileContent(
|
||||
params.absolute_path,
|
||||
this.rootDirectory,
|
||||
this.config.getTargetDir(),
|
||||
params.offset,
|
||||
params.limit,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user