eca icon indicating copy to clipboard operation
eca copied to clipboard

Support code diff/accept change

Open ericdallo opened this issue 1 year ago • 17 comments

Similar to Cursor, where any code change to a file would be presented like a diff which user can accept or not.

Image

I thought about intercepting LLM message looking for code blocks but even so we would need to know the file path somehow, maybe a instruction like "Always provide the filename before the codeblock in this format..." would help, and then we would be able to provide a CodeContent to clients present and do the changes.

ericdallo avatar Aug 04 '25 20:08 ericdallo

Any suggestions @zikajk

ericdallo avatar Aug 04 '25 20:08 ericdallo

FYI @tomgeorge

ericdallo avatar Aug 04 '25 21:08 ericdallo

Added a little context for how two of the popular neovim plugins do it: https://github.com/editor-code-assistant/eca-nvim/issues/9#issuecomment-3153113340

I'm not sure how feasible something like that would be for ECA core 🤷

tomgeorge avatar Aug 05 '25 03:08 tomgeorge

If I'm not mistaken, Cursor or Aider has done it before MCP / Tools. However, in our case, the editing will always be handled by a tool that already knows the file path. That's one less thing to worry about (when manualApproval is on). I've gotten quite used to the Claude flow: check changes after the edit, and if you don't like them, reject them, but this forces you to stage every change you agree with, which is a bit annoying, so it would be great to be able to activate something like Ediff before an edit.

zikajk avatar Aug 05 '25 05:08 zikajk

However, Claude can also display diffs if it is integrated with the IDE via a protocol. Perhaps we could draw inspiration from this as well. It doesn't support Ediff with MCP though - it supports only internal editing tools. What about Cursor, can it deal with MCP edit? https://github.com/manzaltu/claude-code-ide.el https://github.com/manzaltu/claude-code-ide.el/blob/25053b5f1b8123eed5c3f00e8b3e9687ee33391d/screenshots/ediff_diag.png

zikajk avatar Aug 05 '25 05:08 zikajk

@zikajk no, cursor can't. @tomgeorge that's great, thank you very much for this research, we can implement a tool for editting code and that sounds like a good option IMO, with a similar prompt like avante we can leverage that feature!

What we need to improve are the eca builtin tools, currently eca relies a lot on eca_write_file to write the code changes, maybe we shoud: in agent mode we add this existing tool, but disable and add this new eca_edit_code tool when in chat mode? I dunno

ericdallo avatar Aug 05 '25 12:08 ericdallo

After testing cursor, what I noticed is that it edits the file and only later provide a accept/reject option, (the accept just removes the diff overlay as the change was already made, while the reject reverts the change), this is important because without changing the file, the lLM can't have the new content of the file or continue with next moves, (for example clojureMCP eval) so I think we should go with this option:

  • provide a eca_edit_file or eca_edit_code: this will edit the code + return to clients a new CodeChangedContent where they can render diffs/apply/reject etc.

The only thing I'm still wondering is when to provide this tool vs the edit-file built-in tool, maybe we should just remove it and always make it approvable/rejectable via this one like cursor does

ericdallo avatar Aug 05 '25 12:08 ericdallo

  1. By changing the file, do you mean saving it? If so, that seems odd to me - you wouldn't want, for example, your CLJS build to crash if the edit doesn't make sense. I just looked at how Claude Code handles this, and it doesn't save the changes until they are confirmed. But I don't fully understand why the LLM should have the context of the changed file for other tools until you confirm it.

  2. Something to think about -> I've encountered that if you change something in a diff and then approves the changes, it behaves erratically - a good example is this: https://github.com/anthropics/claude-code/issues/1317

  3. Having just one tool sounds good to me (while the diff is skipped when tools are auto-approved).

zikajk avatar Aug 05 '25 14:08 zikajk

  1. yes, cursor does that. I understand your point, my point is: say there is a call to this edit_file tool, what will happen if we don't save the file and LLM calls a read_file tool next in the same response? it would read the outdated content, if we can solve that or accept LLM will consider the new content as the new context, it's OK.

2.yeah that's bad, one way would be when apply the change, client send a diff/applied like request to server, but not sure the whole flow to consider this, would need more thinking indeed.

  1. Ok, will think the next steps.

ericdallo avatar Aug 05 '25 15:08 ericdallo

Also, we won't have control over MCPs doing changes like clojureMCP for example:

Image

You can see it edit the files and then re-read in the same prompt, if we don't save the file we would face issues

ericdallo avatar Aug 05 '25 15:08 ericdallo

I'm leaning towards a simpler approach:

  • only when manualApprove, and LLM is calling eca_edit_file, server will return in the existing toolCallRun content something to know is a code change with a summary, the code and path, with this clients can present a different ux on toolCall blocks to show as a code change approval instead of a simple tool call approve, WDYAT?

ericdallo avatar Aug 05 '25 16:08 ericdallo

protocol changes proposal:

interface ToolCallRunContent {
   ...
    
    /**
     * Extra details about this call. 
     * Clients may use this to present different UX for this tool call.
     */
    details?: TooCallRunDetails;
}

type TooCallRunDetails = FileChangeDetails;

interface FileChangeDetails {
    type: 'fileChange';

     /**
      * The file path of this file change
      */
     path: string;
     
     /**
      * The content diff of this file change
      */
     diff: string;
     
     /**
      * The count of lines added in this change.
      */
     linesAdded: number;
     
     /**
      * The count of lines removed in this change.
      */
     linesRemoved: number;
}

ericdallo avatar Aug 05 '25 16:08 ericdallo

  1. yes, cursor does that. I understand your point, my point is: say there is a call to this edit_file tool, what will happen if we don't save the file and LLM calls a read_file tool next in the same response? it would read the outdated content, if we can solve that or accept LLM will consider the new content as the new context, it's OK.

If the file was saved with the proposed changes and we subsequently rejected them, the following steps could also be incorrect, right? We probably can't avoid this problem. Wouldn't it be best to use an in-memory virtual file that we pass to the following tools instead of changing the file on the disk? Then it would have an up-to-date context, and it wouldn't be so risky.

zikajk avatar Aug 05 '25 18:08 zikajk

Wouldn't it be best to use an in-memory virtual file that we pass to the following tools instead of changing the file on the disk? Then it would have an up-to-date context, and it wouldn't be so risky.

yes, but MCPs that touch filesystem wouldn't follow that, but I believe my proposed solution wouldnt affect that anymore, since it will not save the file, if manualApprove, your original idea I believe.

ericdallo avatar Aug 05 '25 19:08 ericdallo

Yes, the latest proposal seems to me like the best approach :+1:.

zikajk avatar Aug 05 '25 19:08 zikajk

Just shipped on master, will do a release soon, implemented on eca-emacs for now and it looks/works great for eca_write_file and eca_edit_file tools!

Image

ericdallo avatar Aug 05 '25 20:08 ericdallo

Closing this as it's done on 0.16.0

ericdallo avatar Aug 05 '25 21:08 ericdallo