@vfarcic/dot-ai
Version:
AI-powered development productivity platform that enhances software development workflows through intelligent automation and AI-driven assistance
59 lines (58 loc) • 2.36 kB
JavaScript
;
/**
* GitHub Copilot Credential Resolver
*
* Resolves a GitHub token suitable for direct use against api.githubcopilot.com.
* Hermes/dot-ai uses the raw token directly as the Bearer credential — no
* token-exchange step is required and the exchange endpoint
* (api.github.com/copilot_internal/v2/token) can return 404 for some account
* types, so it is intentionally NOT used here.
*
* Supported token types (classic PATs / ghp_* are NOT accepted):
* gho_* OAuth token (recommended — via `gh auth login`)
* github_pat_* Fine-grained PAT (needs Copilot Requests permission)
* ghu_* GitHub App installation token
*
* Token resolution priority:
* 1. GITHUB_COPILOT_TOKEN env var
* 2. GH_TOKEN env var
* 3. GITHUB_TOKEN env var
*
* On HTTP 401, callers should invoke resolve() again to re-read the chain
* (credentials may have been refreshed externally) and retry once.
*
* PRD #587: GitHub Copilot Provider
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeCopilotCredentialResolver = makeCopilotCredentialResolver;
const SUPPORTED_PREFIXES = ['gho_', 'github_pat_', 'ghu_'];
function isSupported(token) {
return SUPPORTED_PREFIXES.some((p) => token.startsWith(p));
}
/**
* Create a CopilotCredentialResolver.
*
* @param overrideToken Optional explicit token (e.g. from env at factory time).
* If provided and supported it is returned immediately without
* inspecting the env chain.
*/
function makeCopilotCredentialResolver(overrideToken) {
return {
resolve() {
// 1. Explicit override (e.g. GITHUB_COPILOT_TOKEN passed by factory)
if (overrideToken && isSupported(overrideToken)) {
return overrideToken;
}
// 2. Env chain
for (const envVar of ['GITHUB_COPILOT_TOKEN', 'GH_TOKEN', 'GITHUB_TOKEN']) {
const val = process.env[envVar];
if (val && isSupported(val)) {
return val;
}
}
throw new Error('No supported GitHub token found for Copilot. ' +
'Set GITHUB_COPILOT_TOKEN (gho_*, github_pat_*, or ghu_*). ' +
'GH_TOKEN and GITHUB_TOKEN are also checked in that order.');
},
};
}