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) return "install";
40 if (provider.auth.status === "unauthenticated") return "signIn";
41 if (provider.status === "ready") return "ready";
42 return "attention";
43}
44
45const PROVIDER_STATE_PRIORITY = {
46 checking: 0,
47 disabled: 1,
48 install: 2,
49 attention: 3,
50 signIn: 4,
51 ready: 5,
52} as const;
53
54/** Select the most usable configured instance for each provider driver. */
55export function selectOnboardingProvidersByDriver(
56 providers: ReadonlyArray<ServerProvider> | null | undefined,
57) {
58 const providersByDriver = new Map<string, ServerProvider>();
59
60 for (const provider of providers ?? []) {
61 const existing = providersByDriver.get(provider.driver);
62 if (
63 existing === undefined ||
64 PROVIDER_STATE_PRIORITY[getOnboardingProviderState(provider)] >
65 PROVIDER_STATE_PRIORITY[getOnboardingProviderState(existing)]
66 ) {
67 providersByDriver.set(provider.driver, provider);
68 }
69 }
70
71 return providersByDriver;
72}
73
74/**
75 * Official standalone installers. Neither needs Node or npm, and both land in
76 * the paths the server's provider maintenance recognizes as native, so the
77 * one-click updater in Settings keeps working after install.
78 */
79const NATIVE_INSTALL_COMMANDS = {
80 claudeAgent: {
81 windows: "irm https://claude.ai/install.ps1 | iex",
82 posix: "curl -fsSL https://claude.ai/install.sh | bash",
83 },
84 codex: {
85 windows: "irm https://chatgpt.com/codex/install.ps1 | iex",
86 posix: "curl -fsSL https://chatgpt.com/codex/install.sh | sh",
87 },
88} as const;
89
90/**
91 * Install command for the setup terminal, keyed on the environment's platform
92 * (not the client's): a Windows desktop driving a WSL server gets the shell
93 * script. Unknown platforms get the shell script too, since the terminal there
94 * is a POSIX shell in practice.
95 */
96export function resolveOnboardingProviderInstallCommand(
97 driver: keyof typeof NATIVE_INSTALL_COMMANDS,
98 platform: ExecutionEnvironmentPlatformOs,
99): string {
100 const commands = NATIVE_INSTALL_COMMANDS[driver];
101 return platform === "windows" ? commands.windows : commands.posix;
102}
103
104/** Use the selected provider instance's binary when the setup terminal opens its login flow. */
105export function resolveOnboardingProviderLoginCommand(
106 provider: ServerProvider,
107 settings: ServerSettings,
108 platform: ExecutionEnvironmentPlatformOs,
109): string {
110 const instance = settings.providerInstances[provider.instanceId];
111
112 if (provider.driver === "claudeAgent") {
113 const config = decodeClaudeSettings(
114 instance ? (instance.config ?? {}) : settings.providers.claudeAgent,
115 );
116 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "claude";
117 return `${quoteProviderBinary(binaryPath, "claude", platform)} auth login`;
118 }
119
120 if (provider.driver === "codex") {
121 const config = decodeCodexSettings(
122 instance ? (instance.config ?? {}) : settings.providers.codex,
123 );
124 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "codex";
125 return `${quoteProviderBinary(binaryPath, "codex", platform)} login`;
126 }
127
128 return provider.driver;
129}