summaryrefslogtreecommitdiff
path: root/init.lua
blob: 32c98debb1cc7e70b16ccb9d548bd112260bae19 (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
69
70
71
72
-- Codex subscription defaults for panto. No login, networking, or session activation.
local auth_defaults = {
    type = "oauth_device",
    dialect = "codex",
    client_id = "app_EMoamEEZ73f0CkXaXp7hrann",
    device_code_url = "https://auth.openai.com/api/accounts/deviceauth/usercode",
    device_poll_url = "https://auth.openai.com/api/accounts/deviceauth/token",
    verification_url = "https://auth.openai.com/codex/device",
    token_url = "https://auth.openai.com/oauth/token",
    account_id_jwt_claim = "https://api.openai.com/auth",
}

local provider_defaults = {
    style = "openai_responses",
    model_catalog_name = "openai",
    extra_headers = {
        ["OpenAI-Beta"] = "responses=experimental",
        originator = "codex_cli_rs",
    },
}

local endpoint_defaults = {
    openai_responses = {
        base_url = "https://chatgpt.com/backend-api/codex",
        dialect = "codex",
    },
}

-- 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 = "codex",
    configure = function(config)
        for _, auth in pairs(config.auth or {}) do
            if type(auth) == "table" and auth.panto_codex_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_codex_extension == true then
                fill_missing(provider, provider_defaults)
                if provider.endpoints == nil then
                    fill_missing(provider, { base_url = endpoint_defaults.openai_responses.base_url })
                    if provider.style == "openai_responses" then
                        fill_missing(provider, { dialect = endpoint_defaults.openai_responses.dialect })
                    end
                else
                    fill_missing(provider, { endpoints = endpoint_defaults })
                end
            end
        end
    end,
}