-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCodexAuthMethod.ts
More file actions
77 lines (66 loc) · 2.22 KB
/
Copy pathCodexAuthMethod.ts
File metadata and controls
77 lines (66 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type {AuthenticateRequest, AuthMethod, ClientCapabilities} from "@agentclientprotocol/sdk";
export const CODEX_API_KEY_ENV_VAR = "CODEX_API_KEY";
export const OPENAI_API_KEY_ENV_VAR = "OPENAI_API_KEY";
interface ApiKeyAuthRequest extends AuthenticateRequest {
methodId: "api-key";
_meta?: {
"api-key"?: {
apiKey: string;
}
} | null;
}
const ApiKeyAuthMethod: AuthMethod = {
id: "api-key",
name: "API Key",
description: "Use an API key to authenticate",
_meta: {
"api-key": {
provider: "openai"
}
}
}
const ChatGptAuthMethod: AuthMethod = {
id: "chat-gpt",
name: "ChatGPT",
description: "Use ChatGPT to authenticate"
}
export interface ChatGPTAuthRequest extends AuthenticateRequest {
methodId: "chat-gpt";
}
export const GatewayAuthMethod = {
id: "gateway",
name: "Custom model gateway",
description: "Use a custom gateway to authenticate and access models",
_meta: {
"gateway": {
protocol: "openai",
restartRequired: "false"
}
}
}
export interface GatewayAuthRequest extends AuthenticateRequest {
methodId: "gateway";
_meta: {
"gateway": {
baseUrl: string;
headers: Record<string, string>;
providerName?: string;
}
};
}
export function getCodexAuthMethods(clientCapabilities?: ClientCapabilities | null, env: NodeJS.ProcessEnv = process.env): AuthMethod[] {
const authMethods: AuthMethod[] = [ApiKeyAuthMethod];
// ChatGPT login requires a browser or URL elicitation support for device code auth
if (!env["NO_BROWSER"] || clientCapabilities?.elicitation?.url) {
authMethods.push(ChatGptAuthMethod);
}
const supportsGatewayAuth = clientCapabilities?.auth?._meta?.["gateway"] === true;
if (supportsGatewayAuth) {
authMethods.push(GatewayAuthMethod);
}
return authMethods;
}
export type CodexAuthRequest = ApiKeyAuthRequest | ChatGPTAuthRequest | GatewayAuthRequest;
export function isCodexAuthRequest(request: AuthenticateRequest): request is CodexAuthRequest {
return request.methodId === "api-key" || request.methodId === "chat-gpt" || request.methodId === "gateway";
}