From fffa06f0b119dfb075ced415a08c517d63dc1953 Mon Sep 17 00:00:00 2001 From: anj-s <32556631+anj-s@users.noreply.github.com> Date: Tue, 3 Jun 2025 08:59:17 -0700 Subject: [PATCH] Modify shortenPath and add param validation (#663) --- packages/core/src/tools/edit.ts | 5 +++++ packages/core/src/utils/paths.ts | 31 ++++++++++++++----------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/core/src/tools/edit.ts b/packages/core/src/tools/edit.ts index 4a337faa..e02ee1c8 100644 --- a/packages/core/src/tools/edit.ts +++ b/packages/core/src/tools/edit.ts @@ -364,16 +364,21 @@ Expectation for required parameters: } getDescription(params: EditToolParams): string { + if (!params.file_path || !params.old_string || !params.new_string) { + return `Model did not provide valid parameters for edit tool`; + } const relativePath = makeRelative(params.file_path, this.rootDirectory); if (params.old_string === '') { return `Create ${shortenPath(relativePath)}`; } + const oldStringSnippet = params.old_string.split('\n')[0].substring(0, 30) + (params.old_string.length > 30 ? '...' : ''); const newStringSnippet = params.new_string.split('\n')[0].substring(0, 30) + (params.new_string.length > 30 ? '...' : ''); + return `${shortenPath(relativePath)}: ${oldStringSnippet} => ${newStringSnippet}`; } diff --git a/packages/core/src/utils/paths.ts b/packages/core/src/utils/paths.ts index bbd479fd..28f2f1f0 100644 --- a/packages/core/src/utils/paths.ts +++ b/packages/core/src/utils/paths.ts @@ -51,14 +51,15 @@ export function shortenPath(filePath: string, maxLen: number = 35): string { } const firstDir = segments[0]; + const lastSegment = segments[segments.length - 1]; const startComponent = root + firstDir; const endPartSegments: string[] = []; - // Base length: startComponent + separator + "..." - let currentLength = startComponent.length + separator.length + 3; + // Base length: separator + "..." + lastDir + let currentLength = separator.length + lastSegment.length; // Iterate backwards through segments (excluding the first one) - for (let i = segments.length - 1; i >= 1; i--) { + for (let i = segments.length - 2; i >= 0; i--) { const segment = segments[i]; // Length needed if we add this segment: current + separator + segment const lengthWithSegment = currentLength + separator.length + segment.length; @@ -67,27 +68,23 @@ export function shortenPath(filePath: string, maxLen: number = 35): string { endPartSegments.unshift(segment); // Add to the beginning of the end part currentLength = lengthWithSegment; } else { - // Adding this segment would exceed maxLen break; } } - // Construct the final path - let result = startComponent + separator + '...'; - if (endPartSegments.length > 0) { - result += separator + endPartSegments.join(separator); + let result = endPartSegments.join(separator) + separator + lastSegment; + + if (currentLength > maxLen) { + return result; } - // As a final check, if the result is somehow still too long (e.g., startComponent + ... is too long) - // fallback to simple truncation of the original path + // Construct the final path + result = startComponent + separator + result; + + // As a final check, if the result is somehow still too long + // truncate the result string from the beginning, prefixing with "...". if (result.length > maxLen) { - const keepLen = Math.floor((maxLen - 3) / 2); - if (keepLen <= 0) { - return filePath.substring(0, maxLen - 3) + '...'; - } - const start = filePath.substring(0, keepLen); - const end = filePath.substring(filePath.length - keepLen); - return `${start}...${end}`; + return '...' + result.substring(result.length - maxLen - 3); } return result;