Skip to content

Conversation

@amittell
Copy link
Contributor

@amittell amittell commented Apr 19, 2025

Add prepend_to_file Tool for Prepending Content to Files

Context

Adding a new prepend_to_file tool to complement the existing append_to_file tool. This provides a more efficient and explicit way to add content to the beginning of files, such as license headers, import statements, and configuration blocks.

Implementation

  • Created a new tool modeled after the existing append_to_file tool with the key difference being content placement
  • Implemented special handling for empty files to prevent unnecessary newlines
  • Added comprehensive tests including various edge cases (empty files, different line endings, license headers)
  • Integrated with existing components:
    • Updated tool registry in src/shared/tools.ts
    • Updated documentation in src/core/prompts/tools/prepend-to-file.ts
    • Updated Cline handler cases in src/core/Cline.ts
    • Updated schema definitions in src/schemas/index.ts
  • Improved overall system documentation to include clear guidance on when to use prepend vs. append tools

Screenshots

No UI changes - this PR adds a new tool accessible through the AI interface.

How to Test

  1. Use the AI to add a license header to a file:

    Add a license header to src/example.js
    
  2. The AI should recommend using the prepend_to_file tool:

    <prepend_to_file>
    <path>src/example.js</path>
    <content>
    /**
     * Copyright (c) 2025 Roo Veterinary Inc.
     * 
     * This source code is licensed under the MIT license found in the
     * LICENSE file in the root directory of this source tree.
     */
    
    </content>
    </prepend_to_file>
    
  3. Verify the content is added to the beginning of the file

  4. Also test with an empty file to verify only the content is added without extra newlines


Important

Introduces prepend_to_file tool to prepend content to files, with integration, testing, and error handling updates.

  • Behavior:
    • Adds prepend_to_file tool to prepend content to files, handling empty files and different line endings.
    • Handles special content formats like code block markers and HTML entities.
  • Integration:
    • Updates tools.ts, index.ts, and rules.ts to include prepend_to_file.
    • Adds getPrependToFileDescription() in prepend-to-file.ts.
  • Testing:
    • Adds prependToFileTool.test.ts for testing various cases like empty files, line endings, and error handling.
  • Error Handling:
    • Validates access using rooIgnoreController and handles missing parameters.

This description was created by Ellipsis for a52ec5d. You can customize this summary. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented Apr 19, 2025

⚠️ No Changeset found

Latest commit: a52ec5d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request labels Apr 19, 2025

pushToolResult(
`The user made the following updates to your content:\n\n${userEdits}\n\n` +
`The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible issue: using relPath.toPosix() assumes that relPath is an object with a .toPosix() method, but it's defined as a string. Consider using a proper path conversion helper (e.g., path.posix.normalize(relPath)) to ensure cross-platform compatibility.

await cline.diffViewProvider.open(relPath)
}

// If file exists, prepend newContent to existing content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider refactoring: there is duplicated logic in handling partial updates versus full updates for prepending content. Extracting the common logic into a helper function would improve maintainability and reduce code duplication.

everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent,
true,
)
await delay(300) // wait for diff view to update
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A hardcoded delay of 300 ms is used after updating the diff view. This may be brittle in some environments; it would be better to either extract this value as a configurable constant or wait for a specific event/acknowledgment from the diff view for updates.

@mrubens
Copy link
Collaborator

mrubens commented Apr 21, 2025

I've been talking to @samhvw8 and we've been discussing just having one insert_content tool that can insert at the beginning, end, or any line within the file (and removing the append_to_file). I think that might be better than having a lot of special case tools, after discussing more. What do you think?

@hannesrudolph hannesrudolph moved this from New to PR [Pre Approval Review] in Roo Code Roadmap Apr 22, 2025
@mrubens
Copy link
Collaborator

mrubens commented Apr 23, 2025

I'm going to close this now that insert_content is available for everyone. Thank you for the contribution and pushing our thinking on this!

@mrubens mrubens closed this Apr 23, 2025
@github-project-automation github-project-automation bot moved this from PR [Pre Approval Review] to Done in Roo Code Roadmap Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants