-
Notifications
You must be signed in to change notification settings - Fork 0
Add GitHub Copilot provider support #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,8 +39,8 @@ import { | |||||||||||||||||
| } from "./utils/config"; | ||||||||||||||||||
| import { | ||||||||||||||||||
| getApiKey as fetchApiKey, | ||||||||||||||||||
| getGithubCopilotApiKey as fetchGithubCopilotApiKey, | ||||||||||||||||||
| maybeRedeemCredits, | ||||||||||||||||||
| fetchGithubCopilotApiKey, | ||||||||||||||||||
| } from "./utils/get-api-key"; | ||||||||||||||||||
| import { createInputItem } from "./utils/input-utils"; | ||||||||||||||||||
| import { initLogger } from "./utils/logger/log"; | ||||||||||||||||||
|
|
@@ -323,12 +323,38 @@ try { | |||||||||||||||||
| if (data.OPENAI_API_KEY && !expired) { | ||||||||||||||||||
| apiKey = data.OPENAI_API_KEY; | ||||||||||||||||||
| } | ||||||||||||||||||
| if ( | ||||||||||||||||||
| data.GITHUBCOPILOT_API_KEY && | ||||||||||||||||||
| provider.toLowerCase() === "githubcopilot" | ||||||||||||||||||
| ) { | ||||||||||||||||||
| apiKey = data.GITHUBCOPILOT_API_KEY; | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| // ignore errors | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if (cli.flags.login) { | ||||||||||||||||||
| if (provider.toLowerCase() === "githubcopilot" && !apiKey) { | ||||||||||||||||||
| apiKey = await fetchGithubCopilotApiKey(); | ||||||||||||||||||
| try { | ||||||||||||||||||
| const home = os.homedir(); | ||||||||||||||||||
| const authDir = path.join(home, ".codex"); | ||||||||||||||||||
| const authFile = path.join(authDir, "auth.json"); | ||||||||||||||||||
| fs.writeFileSync( | ||||||||||||||||||
| authFile, | ||||||||||||||||||
| JSON.stringify( | ||||||||||||||||||
| { | ||||||||||||||||||
| GITHUBCOPILOT_API_KEY: apiKey, | ||||||||||||||||||
| }, | ||||||||||||||||||
| null, | ||||||||||||||||||
| 2, | ||||||||||||||||||
| ), | ||||||||||||||||||
| "utf-8", | ||||||||||||||||||
| ); | ||||||||||||||||||
| } catch { | ||||||||||||||||||
| /* ignore */ | ||||||||||||||||||
| } | ||||||||||||||||||
| } else if (cli.flags.login) { | ||||||||||||||||||
| if (provider.toLowerCase() === "githubcopilot") { | ||||||||||||||||||
| apiKey = await fetchGithubCopilotApiKey(); | ||||||||||||||||||
| } else { | ||||||||||||||||||
|
|
@@ -353,7 +379,7 @@ if (cli.flags.login) { | |||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| // Ensure the API key is available as an environment variable for legacy code | ||||||||||||||||||
| process.env["OPENAI_API_KEY"] = apiKey; | ||||||||||||||||||
| process.env[`${provider.toUpperCase()}_API_KEY`] = apiKey; | ||||||||||||||||||
|
||||||||||||||||||
| process.env[`${provider.toUpperCase()}_API_KEY`] = apiKey; | |
| if (provider.toLowerCase() === "openai") { | |
| process.env["OPENAI_API_KEY"] = apiKey; | |
| } else if (provider.toLowerCase() === "azure") { | |
| process.env["AZURE_API_KEY"] = apiKey; | |
| } else { | |
| process.env[`${provider.toUpperCase()}_API_KEY`] = apiKey; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,12 @@ import type { Choice } from "./get-api-key-components"; | |||||||||||||||
| import type { Request, Response } from "express"; | ||||||||||||||||
|
|
||||||||||||||||
| import { ApiKeyPrompt, WaitingForAuth } from "./get-api-key-components"; | ||||||||||||||||
| import { GithubCopilotClient } from "./openai-client.js"; | ||||||||||||||||
| import Spinner from "../components/vendor/ink-spinner.js"; | ||||||||||||||||
| import chalk from "chalk"; | ||||||||||||||||
| import express from "express"; | ||||||||||||||||
| import fs from "fs/promises"; | ||||||||||||||||
| import { render } from "ink"; | ||||||||||||||||
| import { Box, Text, render } from "ink"; | ||||||||||||||||
| import crypto from "node:crypto"; | ||||||||||||||||
| import { URL } from "node:url"; | ||||||||||||||||
| import open from "open"; | ||||||||||||||||
|
|
@@ -763,26 +765,29 @@ export async function getApiKey( | |||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export { maybeRedeemCredits }; | ||||||||||||||||
|
|
||||||||||||||||
| export async function fetchGithubCopilotApiKey(): Promise<string> { | ||||||||||||||||
| if (process.env["GITHUB_COPILOT_TOKEN"]) { | ||||||||||||||||
| return process.env["GITHUB_COPILOT_TOKEN"]!; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const choice = await promptUserForChoice(); | ||||||||||||||||
| if (choice.type === "apikey") { | ||||||||||||||||
| process.env["GITHUB_COPILOT_TOKEN"] = choice.key; | ||||||||||||||||
| return choice.key; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Sign in via GitHub is not yet supported; instruct the user | ||||||||||||||||
| // eslint-disable-next-line no-console | ||||||||||||||||
| console.error( | ||||||||||||||||
| "\n" + | ||||||||||||||||
| "GitHub OAuth login is not yet implemented for Codex. " + | ||||||||||||||||
| "Please generate a token manually and set it as GITHUB_COPILOT_TOKEN." + | ||||||||||||||||
| "\n" | ||||||||||||||||
| export async function getGithubCopilotApiKey(): Promise<string> { | ||||||||||||||||
|
||||||||||||||||
| export async function getGithubCopilotApiKey(): Promise<string> { | |
| export async function getGithubCopilotApiKey(): Promise<string> { | |
| const choice = await promptUserForChoice(); | |
| if (choice.type === "apikey") { | |
| process.env["GITHUBCOPILOT_API_KEY"] = choice.key; | |
| return choice.key; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The initial
githubcopilotlogin block will always take precedence, making the subsequentcli.flags.loginbranch unreachable for Copilot; consider merging or removing redundant logic.