Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ copilot-relay cache --hourly # or --daily; narrow with --since 6h or --m
copilot-relay cache --json
```

Check your Copilot plan and quota with `copilot-relay usage`; add `--json` for
scripts. It asks GitHub directly with the stored token, so no relay needs to run.

## Go further

**[Open the Wiki](https://github.com/D0n9X1n/copilot-relay/wiki)** · **[English / 中文](wiki/README.md)**
Expand Down
40 changes: 40 additions & 0 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ const readGitHubToken = async () => {
return token.trim()
}

// The stored token, or undefined when there is none, for a command that must not write. Unlike
// readGitHubToken it creates no directory or file and migrates no legacy token.
export const readStoredGitHubToken = async (): Promise<string | undefined> => {
let content: string

try {
content = await fs.readFile(paths.githubTokenPath, "utf8")
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
return undefined
}

throw error
}

const token = content.trim()
registerLogSecret(token)

return token === "" ? undefined : token
}

const writeGitHubToken = async (token: string) => {
registerLogSecret(token.trim())
await ensurePaths()
Expand Down Expand Up @@ -256,6 +277,25 @@ const getCopilotToken = async (
return (await response.json()) as CopilotTokenResponse
}

// The plan and quota behind `copilot-relay usage`. It is read with the GitHub token itself, so no
// Copilot token is exchanged for it. The body is GitHub's internal format; the caller checks it.
export const getCopilotUsage = async (
githubToken: string,
vsCodeVersion: string,
signal?: AbortSignal,
): Promise<unknown> => {
const response = await fetch(`${githubApiBaseUrl}/copilot_internal/user`, {
headers: githubHeaders(githubToken, vsCodeVersion),
signal,
})

if (!response.ok) {
throw new HTTPError("Failed to get Copilot usage", response)
}

return response.json()
}

const isCopilotTokenError = (error: unknown): boolean =>
error instanceof HTTPError && error.message === "Failed to get Copilot token"

Expand Down
2 changes: 1 addition & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AppConfig } from "~/lib/app-config"
import type { CopilotModelCatalog } from "~/copilot/models"
import type { RequestTrace } from "./request-trace"

const vscodeVersion = "1.99.3"
export const vscodeVersion = "1.99.3"

export interface ProxyConfig {
host: string
Expand Down
Loading
Loading