The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { GitHubSettings } from "../shared/definition.ts"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | const GitHubAuthStatusAccountSchema = Schema.Struct({ |
| 6 | state: Schema.String, |
| 7 | error: Schema.optional(Schema.String), |
| 8 | active: Schema.Boolean, |
| 9 | host: Schema.String, |
| 10 | login: Schema.String, |
| 11 | tokenSource: Schema.optional(Schema.String), |
| 12 | }); |
| 13 | |
| 14 | const GitHubAuthStatusSchema = Schema.Struct({ |
| 15 | hosts: Schema.Record(Schema.String, Schema.Array(GitHubAuthStatusAccountSchema)), |
| 16 | }); |
| 17 | |
| 18 | const decodeGitHubAuthStatusJson = Schema.decodeUnknownOption( |
| 19 | Schema.fromJsonString(GitHubAuthStatusSchema), |
| 20 | ); |
| 21 | |
| 22 | export interface GitHubAuthStatusAccount { |
| 23 | readonly host: string; |
| 24 | readonly account: string; |
| 25 | readonly authenticated: boolean; |
| 26 | readonly active: boolean; |
| 27 | readonly error: string | null; |
| 28 | readonly environmentVariable: string | null; |
| 29 | } |
| 30 | |
| 31 | export interface GitHubAuthStatus { |
| 32 | readonly parsed: boolean; |
| 33 | readonly accounts: ReadonlyArray<GitHubAuthStatusAccount>; |
| 34 | } |
| 35 | |
| 36 | const ENVIRONMENT_TOKEN_SOURCE = /^(?:GH|GITHUB)_(?:ENTERPRISE_)?TOKEN$/u; |
| 37 | |
| 38 | function nonEmptyString(value: string): string | null { |
| 39 | const trimmed = value.trim(); |
| 40 | return trimmed.length > 0 ? trimmed : null; |
| 41 | } |
| 42 | |
| 43 | export function parseGitHubAuthStatus(text: string): GitHubAuthStatus { |
| 44 | return Option.match(decodeGitHubAuthStatusJson(text), { |
| 45 | onNone: () => ({ parsed: false, accounts: [] }), |
| 46 | onSome: (status) => |
| 47 | ({ |
| 48 | parsed: true, |
| 49 | accounts: Object.values(status.hosts).flatMap((accounts) => |
| 50 | accounts.flatMap((account) => { |
| 51 | const host = nonEmptyString(account.host); |
| 52 | const login = nonEmptyString(account.login); |
| 53 | if (host === null || login === null) return []; |
| 54 | |
| 55 | return [ |
| 56 | { |
| 57 | host: host.toLowerCase(), |
| 58 | account: login, |
| 59 | authenticated: account.state === "success", |
| 60 | active: account.active, |
| 61 | error: account.error?.trim() || null, |
| 62 | environmentVariable: ENVIRONMENT_TOKEN_SOURCE.test(account.tokenSource ?? "") |
| 63 | ? (account.tokenSource ?? null) |
| 64 | : null, |
| 65 | }, |
| 66 | ]; |
| 67 | }), |
| 68 | ), |
| 69 | }) satisfies GitHubAuthStatus, |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | export function findAuthenticatedGitHubAccount( |
| 74 | accounts: ReadonlyArray<GitHubAuthStatusAccount>, |
| 75 | ): GitHubAuthStatusAccount | undefined { |
| 76 | return ( |
| 77 | accounts.find((account) => account.authenticated && account.active) ?? |
| 78 | accounts.find((account) => account.authenticated) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | export function effectiveGitHubAccount( |
| 83 | host: string, |
| 84 | accounts: ReadonlyArray<GitHubAuthStatusAccount>, |
| 85 | settings: GitHubSettings, |
| 86 | ): GitHubAuthStatusAccount | undefined { |
| 87 | const choice = settings.hosts[host]; |
| 88 | if (choice?.enabled === false) return undefined; |
| 89 | const usable = accounts.filter((account) => account.host === host && account.authenticated); |
| 90 | return ( |
| 91 | usable.find((account) => account.environmentVariable !== null) ?? |
| 92 | (choice?.account === undefined |
| 93 | ? undefined |
| 94 | : usable.find((account) => account.account === choice.account)) ?? |
| 95 | findAuthenticatedGitHubAccount(usable) |
| 96 | ); |
| 97 | } |