Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerReadiness.logic.ts

119 lines3.8 KB

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;
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";
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;
74​
75/**
76 * Neither needs Node or npm, and both land in the paths the server's provider
77 * maintenance recognizes as native, so the one-click updater in Settings keeps
78 * working after install.
79 */
80const NATIVE_INSTALL_COMMANDS = {
81 claudeAgent: {
82 windows: "irm https://claude.ai/install.ps1 | iex",
83 posix: "curl -fsSL https://claude.ai/install.sh | bash",
84 },
85 codex: {
86 windows: "irm https://chatgpt.com/codex/install.ps1 | iex",
87 posix: "curl -fsSL https://chatgpt.com/codex/install.sh | sh",
88 },
89} as const;
90​
91export function resolveOnboardingProviderInstallCommand(
92 driver: keyof typeof NATIVE_INSTALL_COMMANDS,
93 platform: ExecutionEnvironmentPlatformOs,
94): string {
95 const commands = NATIVE_INSTALL_COMMANDS[driver];
96 return platform === "windows" ? commands.windows : commands.posix;
98​
99export function resolveOnboardingProviderLoginCommand(
100 provider: ServerProvider,
101 settings: ServerSettings,
102 platform: ExecutionEnvironmentPlatformOs,
103): string {
104 const instance = settings.providerInstances[provider.instanceId];
105​
106 if (provider.driver === "claudeAgent") {
107 const config = decodeClaudeSettings(instance?.config ?? {});
108 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "claude";
109 return `${quoteProviderBinary(binaryPath, "claude", platform)} auth login`;
110 }
111​
112 if (provider.driver === "codex") {
113 const config = decodeCodexSettings(instance?.config ?? {});
114 const binaryPath = Option.isSome(config) ? config.value.binaryPath : "codex";
115 return `${quoteProviderBinary(binaryPath, "codex", platform)} login`;
116 }
117​
118 return provider.driver;