//// GitHub Copilot as a model provider: GitHub's device sign-in, a pasted //// GitHub token, or GITHUB_TOKEN in the daemon's environment, over the Copilot //// API. The Copilot API is undocumented; see robot-docs/auth.md. //// //// A GitHub token is not the credential requests carry: the daemon mints a //// short-lived Copilot token from it through accounts.gleam, so a session's client //// holds that token and Copilot's own headers, and `wire.gleam` picks the //// route each model is served on. import albedo_ext_github_copilot/accounts import albedo_ext_github_copilot/catalog import albedo_ext_github_copilot/wire import albedo_model as types import albedo_sdk/credentials import albedo_sdk/extension import albedo_sdk/key_environment.{type KeyEnvironment} import albedo_sdk/oauth import albedo_sdk/rotation import gleam/dynamic.{type Dynamic} import gleam/int import gleam/list import gleam/option.{type Option, None, Some} import gleam/result import gleam/string const provider_name = catalog.provider_name pub type Connection { Connection(access: accounts.Access, client: wire.Client) } pub fn extension() -> extension.Extension { extension.Extension( provider_name, "GitHub Copilot models over the Copilot API, through GitHub's device sign-in, a pasted GitHub token or GITHUB_TOKEN; gpt, claude, gemini, grok and mai-code models", ["models"], [ extension.LoginPlugin(login()), extension.ModelsPlugin(catalog.catalog()), extension.ModelProviderPlugin(extension.ModelProvider( provider_name, resolve, None, )), ], extension.no_initialise, tables: [], uses: [], ) } pub fn login() -> oauth.Login { oauth.Login( provider_name, "sign in to github copilot", "uses your copilot subscription", oauth.Model, provider_name, oauth.device(accounts.device_flow), fn(_) { Ok("") }, fn(_, _, _) { Error("GitHub Copilot uses device authorization") }, account, None, ) } fn resolve( context: extension.ModelContext, ) -> Option(Result(extension.Upstream, String)) { use <- rotation.require_provider( context, provider_name, "GitHub Copilot", types.ChatCompletions, ) let pool = pool(context.home, context.profile, context.session, context.keys) use client <- result.map(pool.current()) // `count` stays None: Copilot offers no token-count endpoint on any of its // Chat Completions, Responses or Messages routes. let upstream = rotation.upstream( client.client.connection.base_url, client.client.connection.protocol, pool, client, fn(client, error) { explain_key(context.home, context.profile, context.keys, client, error) }, fn(client) { accounts.label(credentials.value(client.access.account)) }, types.any_images, ) // Claude models are served on the Messages route, which caches only the // prefixes a request marks; Copilot's other routes cache on their own. extension.Upstream(..upstream, cache_marks: wire.cache_marks) } /// The profile's pinned account, explicit rotation, or single plain credential. pub fn pool( home: String, profile: String, session: String, keys: KeyEnvironment, ) -> rotation.Pool(Connection) { rotation.Pool( current: fn() { use access <- result.try(accounts.select(home, profile, session, keys)) case string.trim(access.session.endpoint) { "" -> Error("GitHub Copilot has no API endpoint for this account") _ -> Ok(Connection( access, wire.client( access.session.endpoint, access.session.token, session, catalog.served_endpoints(home, access.session.endpoint), ), )) } }, mark: fn(connection, limit) { accounts.mark(home, profile, keys, connection.access, limit) |> rotation.mark_limit }, same: fn(a, b) { rotation.same_client(a.client.connection, b.client.connection) }, stream: fn(connection, request, on_event) { catalog.catalog().lookup( request.model, Some(connection.client.connection.base_url), ) |> extension.within_listed_efforts(request, _) |> wire.stream(connection.client, _, on_event) }, reject: fn(connection) { accounts.expire(connection.access) }, ) } /// A 429 reaching here has already moved through the profile's rotation members /// that have room. pub fn explain_key( home: String, profile: String, keys: KeyEnvironment, client: Connection, error: types.Error, ) -> Option(String) { case error { types.Refused(types.RateLimited(limit), ..) -> case accounts.mark(home, profile, keys, client.access, limit) { Ok(limit) if limit.next != "" -> Some(rotation.next_turn_message( "GitHub Copilot limit reached until " <> limit.until, limit.next, )) _ -> explain(error) } _ -> explain(error) } } fn explain(error: types.Error) -> Option(String) { case error { types.Refused(types.CredentialRejected, ..) -> Some( "GitHub Copilot rejected this credential; run /login to sign in again, or check GITHUB_TOKEN", ) types.Refused(types.AccountBlocked(types.NotEntitled), _, detail) -> Some(with_detail( "GitHub Copilot refused this request (is the account entitled to the model?)", detail, )) types.Refused(types.ModelUnavailable, ..) -> Some("GitHub Copilot does not serve this model; choose another in /model") types.Refused(types.RateLimited(_), _, detail) -> Some(with_detail("GitHub Copilot request limit reached", detail)) types.Refused(_, Some(status), detail) -> Some(with_detail( "GitHub Copilot error (" <> int.to_string(status) <> ")", detail, )) _ -> None } } fn with_detail(prefix: String, detail: String) -> String { case string.trim(detail) { "" -> prefix detail -> prefix <> ": " <> detail } } /// How /login names a stored account: a GitHub sign-in by its login, or a /// pasted token by its masked label. fn account(credential: Dynamic) -> oauth.Account { case credentials.text(credential, "type"), credentials.text(credential, "apiKey") { "oauth", _ -> { let id = credentials.text(credential, "accountId") let label = [ credentials.text(credential, "login"), credentials.text(credential, "label"), id, ] |> list.find(fn(text) { text != "" }) |> result.unwrap("GitHub Copilot") kind_account(id, label, "github copilot · oauth", credential) } _, "" -> oauth.account("", "invalid GitHub Copilot account", "", False) _, key -> { let id = rotation.key_label(key) let label = case credentials.text(credential, "label") { "" -> id label -> label } kind_account(id, label, "github copilot · token", credential) } } } /// The detail names the kind of credential, and whether it is selected. fn kind_account( id: String, label: String, kind: String, credential: Dynamic, ) -> oauth.Account { case oauth.selected(credential) { True -> oauth.account(id, label, kind <> " · selected", True) False -> oauth.account(id, label, kind, False) } }