copilot-emacs / copilot.el

An unofficial Copilot plugin for Emacs.
MIT License
1.71k stars 122 forks source link

Does this implementation send other project files to copilot context? #227

Closed vkz closed 5 months ago

vkz commented 6 months ago

Could anyone give a definitive answer here, please? A few closed issues I've seen seem to suggest, the answer is no, and in fact only part of the active buffer is being sent as context. But having looked through the code we appear to send a few notifications to whenever file changes or file is open, etc.

Would be very helpful to know how far apart VSC copilot and Emacs.

Thank you

emil-vdw commented 5 months ago

What do you mean by other project files?

Events (open, focus, didChange, etc.) are only sent for buffers where copilot-mode is active (unless global-copilot-mode is active of course).

vkz commented 5 months ago

Here's an example:

/foo/.git
/foo/a.py
/foo/b.py
/foo/c.py

I'm actively working on c.py. I also have buffers visiting a.py and b.py open. All have copilot enabled. When I trigger copilot in c.py, will Github LLM receive additional context from a.py and b.py? In VSCode I'm pretty confident the answer is YES (well, it is a bit more complicated, what with the heuristics they apply, but kinda YES). In Emacs, however, I strongly suspect the answer is NO.

emil-vdw commented 5 months ago

It works a bit differently in copilot.el. Context is sent to the agent as you open, modify, switch to buffers, etc. not when you request a completion.

The flow would look something like this (simplified):

  1. You open a.py copilot sends a didOpen event with the entire contents of the buffer of a.py (up to a maximum amount of copilot-max-char characters).
  2. You open b.py copilot sends a didOpen event with the entire contents of the buffer of b.py (up to a maximum amount of copilot-max-char characters).
  3. You open c.py copilot sends a didOpen event with the entire contents of the buffer of c.py (up to a maximum amount of copilot-max-char characters).
  4. You switch buffers to a.py again copilot sends a didFocus event for buffer a.py
  5. You make a change in buffer a.py copilot sends a didChange event for buffer a.py
  6. You switch back to buffer c.py copilot sends a didFocus event for buffer c.py
  7. You request a completion somewhere in buffer c.py copilot requests a completion at the specific point in the document

The same goes for any other buffer you open, switch to, modify when copilot mode is locally active in that buffer.

All that being said, copilot.el sends events to keep the state of the agent up to date and does not send state when requesting a completion.

vkz commented 5 months ago

This explains the events - very helpful. Without my knowing what those events translate to in the copilot on the server, though, I can't answer the original and main question: when I work on c.py will a.py and b.py be part of the context for completions in c.py? With VSCode the answer is a definitiev YES (with caveats re context window, number of open files and their proprietary heuristics - i.e. maybe only parts of those files will make up the context available to LLM). Think you could shed some light there? That'll be very helpful. Thank you

emil-vdw commented 5 months ago

I am quite certain that, yes, the agent should use the recent context (more than just the most recently focused buffer) for completions. The behaviour should be no different than copilot.vim in that respect.

I.e. it should take into account the content of buffers a.py and b.py when creating completions in buffer c.py given the scenario you outlined above.

vkz commented 5 months ago

Thank you for your answer! This is very good to know. Hope it saves others some head scratching.

Closing