summaryrefslogtreecommitdiff
path: root/init.lua
blob: dcc5fbbea47b81da1db98abf004d2e220726f84d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- GitHub Copilot defaults for panto. No login, networking, or session activation.
local auth_defaults = {
    type = "oauth_device",
    client_id = "Iv1.b507a08c87ecfe98",
    device_code_url = "https://github.com/login/device/code",
    token_url = "https://github.com/login/oauth/access_token",
    scope = "read:user",
    exchange_url = "https://api.github.com/copilot_internal/v2/token",
    exchange_expires_path = "expires_at",
    exchange_base_url_path = "endpoints.api",
}

local provider_defaults = {
    style = "openai_responses",
    model_catalog_name = "github-copilot",
    extra_headers = {
        ["User-Agent"] = "GitHubCopilotChat/0.35.0",
        ["Editor-Version"] = "vscode/1.107.0",
        ["Editor-Plugin-Version"] = "copilot-chat/0.35.0",
        ["Copilot-Integration-Id"] = "vscode-chat",
    },
}

local endpoint_defaults = {
    openai_chat = { base_url = "https://api.individual.githubcopilot.com" },
    openai_responses = { base_url = "https://api.individual.githubcopilot.com" },
    anthropic_messages = { base_url = "https://api.individual.githubcopilot.com" },
}

-- Fill only missing values; HTTP header names are case-insensitive.
local function fill_missing(target, defaults, headers)
    for key, value in pairs(defaults) do
        local target_key = key
        if headers then
            for existing in pairs(target) do
                if type(existing) == "string" and existing:lower() == key:lower() then
                    target_key = existing
                    break
                end
            end
        end
        if target[target_key] == nil then
            target[target_key] = type(value) == "table" and {} or value
        end
        if type(value) == "table" and type(target[target_key]) == "table" then
            fill_missing(target[target_key], value, key == "extra_headers")
        end
    end
end

return {
    name = "copilot",
    configure = function(config)
        for _, auth in pairs(config.auth or {}) do
            if type(auth) == "table" and auth.panto_copilot_extension == true then
                fill_missing(auth, auth_defaults)
            end
        end
        for _, provider in pairs(config.providers or {}) do
            if type(provider) == "table" and provider.panto_copilot_extension == true then
                fill_missing(provider, provider_defaults)
                if provider.base_url == nil then
                    fill_missing(provider, { endpoints = endpoint_defaults })
                end
            end
        end
    end,
}