copilot

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultClientID    = "Iv1.b507a08c87ecfe98"
	DefaultScope       = "copilot"
	DeviceCodeEndpoint = "https://github.com/login/device/code"
	OAuthTokenEndpoint = "https://github.com/login/oauth/access_token"
)
View Source
const (
	CopilotTokenEndpoint = "https://api.github.com/copilot_internal/v2/token"
	CopilotAPIEndpoint   = "https://api.githubcopilot.com"
)

Variables

View Source
var ErrModelsNotFound = errors.New("copilot: no models file")
View Source
var ErrNotFound = errors.New("copilot: no token file")

ErrNotFound is returned by Load when no token file exists.

Functions

func DefaultModelsPath

func DefaultModelsPath() (string, error)

func DefaultStorePath

func DefaultStorePath() (string, error)

DefaultStorePath returns ~/.yottacode/auth/copilot.json.

func IsChatModel

func IsChatModel(id string) bool

IsChatModel returns true if the model ID looks like a chat/completion model rather than an internal routing entry, embedding model, or other non-chat artifact. The Copilot /models endpoint returns everything the account has access to, including entries like "accounts/msft/routers/...", "text-embedding-*", and internal tool names that aren't usable for chat completions.

func Login

func Login(ctx context.Context, opts LoginOptions) (string, error)

Login runs the full device code flow: request a code, wait for the user to authorize, return the GitHub OAuth token.

func PollForToken

func PollForToken(ctx context.Context, httpClient *http.Client, clientID string, dc DeviceCode) (string, error)

PollForToken polls the OAuth token endpoint until the user completes the device authorization or the code expires.

func Save

func Save(path string, ts TokenSet) error

Save writes ts to path with mode 0600 atomically.

func SaveModels

func SaveModels(path string, mf ModelsFile) error

Types

type CachedModel

type CachedModel struct {
	ID            string `json:"id"`
	Name          string `json:"name,omitempty"`
	ContextWindow int    `json:"context_window,omitempty"`
	MaxOutput     int    `json:"max_output,omitempty"`
	Disabled      bool   `json:"disabled,omitempty"`
}

CachedModel is one entry in the persisted models file.

func FetchAndCacheModels

func FetchAndCacheModels(ctx context.Context, ct CopilotToken) ([]CachedModel, error)

FetchAndCacheModels fetches the model list from the Copilot API, filters to chat models, and persists the result to the default models path. Shared by the CLI, TUI inline auth, and wizard flows.

func FetchModels

func FetchModels(ctx context.Context, ct CopilotToken) ([]CachedModel, error)

FetchModels queries the Copilot /models endpoint and returns filtered chat models with plan state.

type CopilotToken

type CopilotToken struct {
	Token     string `json:"token"`
	ExpiresAt int64  `json:"expires_at"`
	Endpoints struct {
		API string `json:"api"`
	} `json:"endpoints"`
}

CopilotToken is the short-lived API token returned by the Copilot token endpoint. Typically expires in ~30 minutes.

func FetchCopilotToken

func FetchCopilotToken(ctx context.Context, httpClient *http.Client, githubToken string) (CopilotToken, error)

FetchCopilotToken exchanges a GitHub OAuth token for a short-lived Copilot API token.

func FetchCopilotTokenFrom

func FetchCopilotTokenFrom(ctx context.Context, httpClient *http.Client, githubToken, endpoint string) (CopilotToken, error)

FetchCopilotTokenFrom is the test-friendly form that accepts a custom endpoint URL.

func (CopilotToken) ExpiresAtTime

func (ct CopilotToken) ExpiresAtTime() time.Time

ExpiresAtTime returns ExpiresAt as a time.Time.

type DeviceCode

type DeviceCode struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int    `json:"expires_in"`
	Interval        int    `json:"interval"`
}

DeviceCode is the response from the device code initiation request.

func RequestDeviceCode

func RequestDeviceCode(ctx context.Context, httpClient *http.Client, clientID string) (DeviceCode, error)

RequestDeviceCode initiates the device code flow.

type LoginOptions

type LoginOptions struct {
	ClientID   string
	HTTPClient *http.Client

	// OnDeviceCode is called with the device code response after
	// initiation. The caller should display the user_code and
	// verification_uri to the user.
	OnDeviceCode func(dc DeviceCode)
}

LoginOptions tunes the device code flow.

type ModelsFile

type ModelsFile struct {
	CachedAt time.Time     `json:"cached_at"`
	Models   []CachedModel `json:"models"`
}

ModelsFile is the on-disk cache of available Copilot models.

func LoadModels

func LoadModels(path string) (ModelsFile, error)

type TokenSet

type TokenSet struct {
	GitHubToken string    `json:"github_token"`
	ExpiresAt   time.Time `json:"expires_at,omitempty"`
}

TokenSet is the on-disk shape of a successful device-code login. Stores the long-lived GitHub OAuth token. The short-lived Copilot API token is cached in memory by TokenSource, not persisted.

func InlineLogin

func InlineLogin(ctx context.Context, opts LoginOptions) (TokenSet, error)

InlineLogin runs the device code flow and persists the token to the default store path.

func Load

func Load(path string) (TokenSet, error)

Load reads the token file at path.

type TokenSource

type TokenSource struct {
	// contains filtered or unexported fields
}

TokenSource is a thread-safe accessor for the Copilot API token. It lazy-loads the GitHub OAuth token from disk, exchanges it for a short-lived Copilot token, and caches/refreshes the Copilot token as needed. The GitHub token is long-lived; the Copilot token typically expires in ~30 minutes.

func NewTokenSource

func NewTokenSource(path string) *TokenSource

NewTokenSource returns a TokenSource backed by the file at path.

func (*TokenSource) APIEndpoint

func (s *TokenSource) APIEndpoint() string

APIEndpoint returns the Copilot API base URL from the last token exchange. Defaults to CopilotAPIEndpoint if no exchange has happened yet.

func (*TokenSource) ForceRefresh

func (s *TokenSource) ForceRefresh(ctx context.Context) error

ForceRefresh unconditionally re-fetches the Copilot token. Used on 401 responses from the API.

func (*TokenSource) GitHubToken

func (s *TokenSource) GitHubToken() (string, error)

GitHubToken returns the stored GitHub OAuth token for status display.

func (*TokenSource) SetHTTPClient

func (s *TokenSource) SetHTTPClient(c *http.Client)

SetHTTPClient overrides the http.Client used for token exchange.

func (*TokenSource) SetTokenEndpoint

func (s *TokenSource) SetTokenEndpoint(url string)

SetTokenEndpoint overrides the Copilot token endpoint (for tests).

func (*TokenSource) Token

func (s *TokenSource) Token(ctx context.Context) (string, error)

Token returns a valid Copilot API bearer token. If the cached Copilot token is expired, it exchanges the GitHub token for a fresh one. ErrNotFound surfaces when no token file exists at the configured path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL