opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Github Copilot Support

Open davinpkn opened this issue 1 year ago • 21 comments

Setting up as Open AI work, but Antrophic models doesn't shows up.

https://aider.chat/docs/llms/github.html

davinpkn avatar Jun 16 '25 16:06 davinpkn

@davioliveira-dev how are you getting gh copilot to work? did you do a custom opencode.json? If so, how did you populate it to get it to work? Thanks!

GitMurf avatar Jun 16 '25 21:06 GitMurf

attempted configuring an opencode.json with no luck, would love to see github copilot as a provider option as it is free for students. @davioliveira-dev if you don't mind could you share how you achieved this configuration with opencode? Thanks!

timotah avatar Jun 18 '25 01:06 timotah

An example would be awesome I was just saying to someone at work that I want to try this exact setup.

mhuggins7278 avatar Jun 18 '25 01:06 mhuggins7278

This is what worked for me:

  1. generate a token with a jetbrains IDE (https://aider.chat/docs/llms/github.html#where-do-i-get-the-token) I got api warnings when using the token that was generated with neovims copilot.lua plugin You can use a EAP version like https://www.jetbrains.com/webstorm/nextversion/, install the copilot plugin and login
  2. Add copilot to the auth.json file
  3. find the models you want to use https://aider.chat/docs/llms/github.html#discover-available-models
  4. add the models to an opencode.json file in your project folder

2: Add copilot to the auth.json file

~/.local/share/opencode/auth.json

{
    "copilot": {
        "type": "api",
        "key": "{{ paste oauth token from ~/.config/github-copilot/apps.json here }}"
    }
}

4: Add the models to an opencode.json file in your project folder

Add the models to the opencode.json file in your project folder

{
    "$schema": "https://opencode.ai/config.json",
    "provider": {
        "copilot": {
            "npm": "@ai-sdk/openai-compatible",
            "options": {
                "baseURL": "https://api.githubcopilot.com"
            },
            "models": {
                "gpt-4o": {
                    "name": "gpt-4o"
                },
                "claude-3.7-sonnet": {
                    "name": "claude-3.7-sonnet"
                }
            }
        }
    }
}

thijsvdanker avatar Jun 18 '25 06:06 thijsvdanker

tried to attempt this with the new config.json format and was unsuccessful, this is what i attempted

{
  "$schema": "http://opencode.ai/config.json"
  "theme": "opencode",
  "model": "copilot/openai/gpt-4o",
  "autoshare": false,
  "autoupdate": true,
  "provider": {
    "copilot": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.githubcopilot.com"
      },
      "models": {
        "openai/gpt-4o": {
          "name": "gpt-4o"
        },
        "anthropic/claude-3.7-sonnet": {
          "name": "claude-3.7-sonnet"
        }
      }
    }
  }
}

used copilot as the "Other" provider on opencode auth login. and it seems opencode only registers openai as my provider and doesn't recognize copilot anymore

timotah avatar Jun 19 '25 11:06 timotah

i tried the above config and it worked for me - in the modal selector are you using left/right arrow keys to go through different providers?

thdxr avatar Jun 19 '25 13:06 thdxr

I did at the time and there was no option when using /models to hit the left/right arrow keys to see the 2 copilot models listed, only showed OpenAI provider models list. I will clear my config and try a fresh install. Looking back now I also see a syntax error in the config I posted, so high likelihood it's that assuming.

timotah avatar Jun 19 '25 13:06 timotah

can confirm it is working, was an error in my config transitioning from the .toml format

timotah avatar Jun 19 '25 18:06 timotah

@timotah @thijsvdanker for copilot provider are you only adding the baseurl option? I am getting a "Forbidden" api response which in my past history typically is due to not passing certain headers required by copilot api such as editor-version etc. How are you getting around this? Where is your copilot apps.json oauth_token coming from(neovim copilot.lua, an IDE, etc.)? Thanks!

GitMurf avatar Jun 19 '25 19:06 GitMurf

@GitMurf I used the GitHub token found via ~/.config/github-copilot/apps.json. This is generated from IntelliJ CE. I used the link to aider documentation in the initial comment to start this issue by @davioliveira-dev. I have not tried any other way due to cautioning against using neovim's generated token. Only adding the baseURL option in config as well.

timotah avatar Jun 19 '25 19:06 timotah

This is generated from IntelliJ CE.

ahh ok, yeah so it seems jetbrains IDEs have an auth token that works without the different vscode spoofed headers etc. I will warn you though based on my past testing that you will have half the context window (64k instead of 128k). It seems to be a limitation that github puts on non vscode based api calls.

Thanks for the quick response!!

GitMurf avatar Jun 19 '25 19:06 GitMurf

attempted configuring an opencode.json with no luck, would love to see github copilot as a provider option as it is free for students. @davioliveira-dev if you don't mind could you share how you achieved this configuration with opencode? Thanks!

Sorry for the late response! I just set as openai compatible and worked. But the @thijsvdanker version using config file is better.

davinpkn avatar Jun 19 '25 22:06 davinpkn

Now, i'm getting the forbidden error like @GitMurf :(

davinpkn avatar Jun 21 '25 15:06 davinpkn

Now, i'm getting the forbidden error like @GitMurf :(

What auth key are you using. It HAS to be from a jetbrains IDE and can NOT be one from like neovim copilot.lua etc. Where are you getting your auth token?

GitMurf avatar Jun 21 '25 17:06 GitMurf

I will warn you though based on my past testing that you will have half the context window (64k instead of 128k). It seems to be a limitation that github puts on non vscode based api calls.

Bummer! I was hoping I was wrong on this and/or something had changed since the last time I ran into this issue, but I just confirmed that indeed copilot tokens coming from Jetbrains IDEs have half the context window (64k). See screenshot below.

Image

GitMurf avatar Jun 21 '25 23:06 GitMurf

@thdxr do we need an oauth setup like we do for anthropic here?

adamdotdevin avatar Jun 22 '25 00:06 adamdotdevin

@thdxr do we need an oauth setup like we do for anthropic here?

If you were thinking about adding "officially" GitHub copilot as a provider for opencode, the Block Goose handles refreshing the copilot token, auth etc so could be good to get a code example from. See here: https://github.com/block/goose/blob/main/crates/goose/src/providers/githubcopilot.rs

GitMurf avatar Jun 22 '25 01:06 GitMurf

Also @adamdotdevin for a quick and dirty of how to refresh the token for copilot chat api (expires every 30 min) I put together both a powershell and shell script version in a Aider gh issue a while back: https://github.com/Aider-AI/aider/issues/2227#issuecomment-2881098223

You can also look at copilot.lua, CopilotChat.nvim, and CodeCompanion neovim plugins who all do variations of initial auth and refreshing tokens.

Hope this helps!

GitMurf avatar Jun 22 '25 01:06 GitMurf

In order to use GitHub Copilot with the configuration outlined here I only had to change the names from

...
"anthropic/claude-3.7-sonnet": {
          "name": "claude-3.7-sonnet"
        }

to something like:

"claude-3.7-sonnet": {
          "name": "claude-3.7-sonnet"
 }

Then it worked.

In any case, I think @adamdotdevin is right and a OAuth authentication flow is needed, to not relay on a third party applications token.

I implemented such authentication flow and opened a PR - Of course with help of 🤖 opencode.

Let me know what you think.

palmamartin avatar Jun 22 '25 07:06 palmamartin

released in 0.1.120

thdxr avatar Jun 23 '25 00:06 thdxr