Skip to content

Commit

Permalink
Merge pull request #214 from sirji-ai/task/bug_fix
Browse files Browse the repository at this point in the history
added check when file path is absolute
  • Loading branch information
kedarchandrayan authored Jul 30, 2024
2 parents a7f360c + 988c27f commit f92c6e0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion sirji/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sirji",
"displayName": "Sirji",
"description": "Sirji is an AI Software Development Agent.",
"version": "0.0.30",
"version": "0.0.31-beta.1",
"publisher": "TrueSparrow",
"icon": "out/assets/sirji.png",
"repository": {
Expand Down
10 changes: 8 additions & 2 deletions sirji/vscode-extension/src/utils/executor/create_file.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { getFilePath } from './helper';

export function sanitizePath(filePath: string): string {
return filePath.replace(/^['"]|['"]$/g, '');
}

export async function createFile(rootPath: string, isProjectRoot: boolean, body: string): Promise<string> {
export async function createFile(rootPath: string, isProjectRoot: boolean, body: string): Promise<any> {
try {
const [filePathPart, fileContent] = body.split('---');
const filePath = filePathPart.replace('File path:', '').trim();
const sanitizedFilePath = sanitizePath(filePath);

const fullPath = path.isAbsolute(sanitizedFilePath) ? sanitizedFilePath : path.join(rootPath, sanitizedFilePath);
let fullPath = '';
try {
fullPath = getFilePath(sanitizedFilePath, rootPath);
} catch (error) {
return error;
}
const uri = vscode.Uri.file(fullPath);

if (isProjectRoot) {
Expand Down
16 changes: 14 additions & 2 deletions sirji/vscode-extension/src/utils/executor/execute_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { getFilePath } from './helper';

let currentTaskExecution: any = null;

Expand All @@ -27,8 +28,19 @@ export async function executeTask(command: string, projectRootPath: string): Pro
return new Promise(async (resolve, reject) => {
const tempFileName = `output_${Date.now()}.txt`;

const tempFileRelativePath = path.join(projectRootPath, tempFileName);
const tempFilePath = path.join(projectRootPath, tempFileRelativePath);
let tempFileRelativePath = '';
try {
tempFileRelativePath = getFilePath(tempFileName, projectRootPath);
} catch (error) {
return error;
}

let tempFilePath = '';
try {
tempFilePath = getFilePath(tempFileRelativePath, projectRootPath);
} catch (error) {
return error;
}

let tempFileContent = '';

Expand Down
11 changes: 9 additions & 2 deletions sirji/vscode-extension/src/utils/executor/fetch_recipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { getFilePath } from './helper';

export async function fetchRecipe(FolderPath: string, body: string): Promise<string> {
export async function fetchRecipe(FolderPath: string, body: string): Promise<any> {
try {
const filePath = body.split('File path:')[1].trim();
let fullFilePath = '';
try {
fullFilePath = getFilePath(filePath, FolderPath);
} catch (error) {
return error;
}

const uri = vscode.Uri.file(path.join(FolderPath, filePath));
const uri = vscode.Uri.file(fullFilePath);

if (!fs.existsSync(uri.fsPath)) {
return 'Index not found';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import * as fs from 'fs';
import path from 'path';
import { getFilePath } from './helper';

const escapeRegExp = (string: string) => {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
Expand Down Expand Up @@ -30,7 +31,15 @@ export const findAndReplaceInProjectFile = async (body: string, projectRootPath:
return `The error in parsing the BODY: ${error}. Either the FILE_PATH key is missing or not in the correct format. The correct format is FILE_PATH: {{File path}} ---. Your response must conform strictly to FIND_AND_REPLACE Response Template with all the keys present in the BODY`;
}

filePath = path.join(projectRootPath, filePath);
console.log('File Path - projectRootPath', projectRootPath);

console.log('File Path - projectRootPath', filePath);

try {
filePath = getFilePath(filePath, projectRootPath);
} catch (error) {
return error;
}

console.log(`Searching for files with pattern: ${searchText} in folder: ${filePath} and replacing with: ${replacement}`);

Expand Down
13 changes: 13 additions & 0 deletions sirji/vscode-extension/src/utils/executor/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';

export const getFilePath = (filePath: string, projectRootPath: string): string => {
if (path.isAbsolute(filePath)) {
if (filePath.includes(projectRootPath)) {
return filePath;
} else {
throw new Error('Error: File does not exist in the project root path');
}
} else {
return path.join(projectRootPath, filePath);
}
};
11 changes: 8 additions & 3 deletions sirji/vscode-extension/src/utils/executor/insert_text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import * as fs from 'fs';
import path from 'path';
import { getFilePath } from './helper';

function delay(ms: number) {
return new Promise((res) => setTimeout(res, ms));
Expand Down Expand Up @@ -40,7 +41,7 @@ async function insertCode(document: vscode.TextDocument, editor: vscode.TextEdit
});
}

export const insertText = async (body: string, projectRootPath: string, insertPosition: string, globPattern?: string, exclude: string = '**/node_modules/**'): Promise<string> => {
export const insertText = async (body: string, projectRootPath: string, insertPosition: string, globPattern?: string, exclude: string = '**/node_modules/**'): Promise<any> => {
let searchText, textToInsert, filePath;

if (insertPosition.toLowerCase() === 'below') {
Expand Down Expand Up @@ -75,7 +76,11 @@ export const insertText = async (body: string, projectRootPath: string, insertPo
return `The error in parsing the BODY: ${error}. Either the FILE_PATH key is missing or not in the correct format. The correct format is FILE_PATH: {{File path without any special characaters}} ---. Your response must conform strictly to Response Template with all the keys present in the BODY`;
}

filePath = path.join(projectRootPath, filePath);
try {
filePath = getFilePath(filePath, projectRootPath);
} catch (error) {
return error;
}

console.log(`Inserting text into file: ${filePath} based on search text: '${searchText}' with replacement: '${textToInsert}' and insert position: '${insertPosition}'`);

Expand All @@ -99,7 +104,7 @@ export const insertText = async (body: string, projectRootPath: string, insertPo

for (let i = 0; i < lines.length; i++) {
const block = lines.slice(i, i + normalizedSearchText.split('\n').length).join('\n');

if (normalizeIndentation(block).includes(normalizedSearchText)) {
console.log('Block found:', block);
startIndex = text.indexOf(block);
Expand Down
10 changes: 8 additions & 2 deletions sirji/vscode-extension/src/utils/executor/read_content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import * as fs from 'fs/promises';
import * as mimetype from 'mime-types';
import { sanitizePath } from './create_file';
import { getFilePath } from './helper';

const SKIP_LIST = ['__pycache__', '.git', '.github', '.gitlab', '.vscode', '.idea', 'node_modules', '.DS_Store', 'venv', '.venv', '.sass-cache', 'dist', 'out', 'build', 'logs', '.npm', 'temp', 'tmp'];

Expand Down Expand Up @@ -32,7 +33,7 @@ function isPathInsideRoot(rootPath: string, targetPath: string): boolean {
return resolvedTarget.startsWith(resolvedRoot);
}

export async function readContent(projectRootPath: string, body: string, isDirectory: boolean): Promise<string> {
export async function readContent(projectRootPath: string, body: string, isDirectory: boolean): Promise<any> {
async function shouldSkip(name: string): Promise<boolean> {
return SKIP_LIST.includes(name);
}
Expand Down Expand Up @@ -107,7 +108,12 @@ export async function readContent(projectRootPath: string, body: string, isDirec

for (const inputPath of inputPaths) {
try {
const fullPath = path.isAbsolute(inputPath) ? inputPath : path.join(projectRootPath, inputPath);
let fullPath = '';
try {
fullPath = getFilePath(inputPath, projectRootPath);
} catch (error) {
return error;
}

if (!isPathInsideRoot(projectRootPath, fullPath)) {
throw new Error(`Path '${inputPath}' is not within the project root.`);
Expand Down

0 comments on commit f92c6e0

Please sign in to comment.