summaryrefslogtreecommitdiff
path: root/packages/core/src
diff options
context:
space:
mode:
authoranj-s <[email protected]>2025-06-03 08:59:17 -0700
committerGitHub <[email protected]>2025-06-03 08:59:17 -0700
commitfffa06f0b119dfb075ced415a08c517d63dc1953 (patch)
tree09656d53ea4f37a4565f46322312f6f4a797262b /packages/core/src
parente9d43b938833f239489a4ff8c70e2701bf178211 (diff)
Modify shortenPath and add param validation (#663)
Diffstat (limited to 'packages/core/src')
-rw-r--r--packages/core/src/tools/edit.ts5
-rw-r--r--packages/core/src/utils/paths.ts31
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;