Open History to see the last commit
1import {
2 ClaudeSettings,
3 CodexSettings,
4 type ExecutionEnvironmentPlatformOs,
5 type ServerProvider,
6 type ServerSettings,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import * as Schema from "effect/Schema";
10
11const decodeClaudeSettings = Schema.decodeUnknownOption(ClaudeSettings);
12const decodeCodexSettings = Schema.decodeUnknownOption(CodexSettings);
13const SAFE_SHELL_BINARY_PATTERN = /^[A-Za-z0-9_./:\\-]+$/;
14
15function quoteProviderBinary(
16 binaryPath: string,
17 fallback: string,
18 platform: ExecutionEnvironmentPlatformOs,
19): string {
20 if (
21 SAFE_SHELL_BINARY_PATTERN.test(binaryPath) &&
22 (platform === "windows" || !binaryPath.includes("\\"))
23 ) {
24 return binaryPath;
25 }
26 if (platform === "windows") return `& '${binaryPath.replaceAll("'", "''")}'`;
27 if (platform === "darwin" || platform === "linux") {
28 if (binaryPath.startsWith("~/") || binaryPath.startsWith("~\\")) {
29 return `~/'${binaryPath.slice(2).replaceAll("'", `'"'"'`)}'`;
30 }
31 return `'${binaryPath.replaceAll("'", `'"'"'`)}'`;
32 }
33 return fallback;
34}
35
36export function getOnboardingProviderState(provider: ServerProvider | undefined) {
37 if (provider === undefined) return "checking";
38 if (!provider.enabled || provider.status === "disabled") return "disabled";
39 if (!provider.installed && provider.status === "warning" && provider.auth.status === "unknown")
40 return "checking";
41 if (!provider.installed) return "install";
42 if (provider.auth.status === "unauthenticated") return "signIn";
43 if (provider.status === "ready") return "ready";
44 return "attention";
45}
46
47const PROVIDER_STATE_PRIORITY = {
48 checking: 0,
49 disabled: 1,
50 install: 2,
51 attention: 3,
52 signIn: 4,
53 ready: 5,
54} as const;
55
56export function selectOnboardingProvidersByDriver(
57 providers: ReadonlyArray<ServerProvider> | null | undefined,
58) {
59 const providersByDriver = new Map<string, ServerProvider>();
60
61 for (const provider of providers ?? []) {
62 const existing = providersByDriver.get(provider.driver);
63 if (
64 existing === undefined ||
65 PROVIDER_STATE_PRIORITY[getOnboardingProviderState(provider)] >
66 PROVIDER_STATE_PRIORITY[getOnboardingProviderState(existing)]
67 ) {
68 providersByDriver.set(provider.driver, provider);
69 }
70 }
71
72 return providersByDriver;
73}
74
75// These need no Node/npm and install where provider maintenance recognizes native binaries.
76const NATIVE_INSTALL_COMMANDS = {
77 claudeAgent: {
78 windows: "irm https://claude.ai/install.ps1 | iex",
79 posix: "curl -fsSL https://claude.ai/install.sh | bash",
80 },
81 codex: {
82 windows: "irm https://chatgpt.com/codex/install.ps1 | iex",
83 posix: "curl -fsSL https://chatgpt.com/codex/install.sh | sh",
84 },
85} as const;
86
87export function resolveOnboardingProviderInstallCommand(
88 driver: keyof typeof NATIVE_INSTALL_COMMANDS,
89 platform: ExecutionEnvironmentPlatformOs,
90): string {
91 const commands = NATIVE_INSTALL_COMMANDS[driver];
92 return platform === "windows" ? commands.windows : commands.posix;
93}
94
95export function resolveOnboardingProviderLoginCommand(
96 provider: ServerProvider,
97 settings: ServerSettings,
98 platform: ExecutionEnvironmentPlatformOs,
99): string {
100 const instance = settings.providerInstances[provider.instanceId];
101
102 if (provider.driver === "claudeAgent") {
103 const config = decodeClaudeSettings(instance?.config ?? {});
104 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "claude";
105 return `${quoteProviderBinary(binaryPath, "claude", platform)} auth login`;
106 }
107
108 if (provider.driver === "codex") {
109 const config = decodeCodexSettings(instance?.config ?? {});
110 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "codex";
111 return `${quoteProviderBinary(binaryPath, "codex", platform)} login`;
112 }
113
114 return provider.driver;
115}