The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { pipe } from "effect/Function"; |
| 2 | import { |
| 3 | DEFAULT_MODEL_BY_PROVIDER, |
| 4 | defaultInstanceIdForDriver, |
| 5 | resolveProjectProviderInstanceEnabled, |
| 6 | type ModelSelection, |
| 7 | type ProjectId, |
| 8 | type ProviderDriverKind, |
| 9 | ProviderInstanceId, |
| 10 | type ServerProvider, |
| 11 | type ServerProviderModel, |
| 12 | type ServerSettings, |
| 13 | type ServerProviderState, |
| 14 | } from "@t3tools/contracts"; |
| 15 | import { |
| 16 | normalizeProviderAccentColor, |
| 17 | resolveProviderInstanceDisplayName, |
| 18 | shouldShowInstanceBadge, |
| 19 | } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 20 | |
| 21 | export { normalizeProviderAccentColor, shouldShowInstanceBadge }; |
| 22 | |
| 23 | /** Placeholder for untargetable drafts. Never persist or dispatch it; disable send until replaced. */ |
| 24 | export const NO_PROVIDER_MODEL_SELECTION: ModelSelection = { |
| 25 | instanceId: ProviderInstanceId.make("t3code_no_provider"), |
| 26 | model: "", |
| 27 | }; |
| 28 | |
| 29 | export interface ProviderInstanceEntry { |
| 30 | readonly instanceId: ProviderInstanceId; |
| 31 | readonly driverKind: ProviderDriverKind; |
| 32 | readonly displayName: string; |
| 33 | readonly accentColor?: string | undefined; |
| 34 | readonly acpRegistryAgentId?: string | undefined; |
| 35 | readonly acpRegistryIconUrl?: string | undefined; |
| 36 | readonly continuationGroupKey?: string | undefined; |
| 37 | readonly enabled: boolean; |
| 38 | readonly installed: boolean; |
| 39 | readonly status: ServerProviderState; |
| 40 | readonly isDefault: boolean; |
| 41 | readonly isAvailable: boolean; |
| 42 | readonly snapshot: ServerProvider; |
| 43 | readonly models: ReadonlyArray<ServerProviderModel>; |
| 44 | } |
| 45 | |
| 46 | /** Status dots share one corner per user, so a badge on one row can't move only that row's dot. */ |
| 47 | export function hasInstanceBadge(entries: ReadonlyArray<ProviderInstanceEntry>): boolean { |
| 48 | return entries.some((entry) => shouldShowInstanceBadge(entry, entries)); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Disabled instances may retain ready status until reconciliation. A warning still offers |
| 53 | * the models of sub-providers that report no problem of their own. |
| 54 | */ |
| 55 | function isProviderInstanceHealthy(entry: ProviderInstanceEntry): boolean { |
| 56 | if (!entry.enabled || !entry.isAvailable) return false; |
| 57 | if (entry.status === "ready") return true; |
| 58 | if (entry.status !== "warning") return false; |
| 59 | const usable = new Set( |
| 60 | entry.snapshot.subProviders?.flatMap((subProvider) => |
| 61 | subProvider.unavailableReason === undefined ? [subProvider.name] : [], |
| 62 | ), |
| 63 | ); |
| 64 | return entry.models.some( |
| 65 | (model) => model.subProvider !== undefined && usable.has(model.subProvider), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** Also true after a check that only failed or ran late: an earlier check's models stay offered. */ |
| 70 | export function isProviderInstancePickerReady(entry: ProviderInstanceEntry): boolean { |
| 71 | if (isProviderInstanceHealthy(entry)) return true; |
| 72 | const { subProviders, auth, modelsRetained } = entry.snapshot; |
| 73 | if (!entry.enabled || !entry.isAvailable || subProviders !== undefined) return false; |
| 74 | if (modelsRetained === true) return true; |
| 75 | // The sign-in was confirmed, so the warning is about something else, such as a late catalog. |
| 76 | return ( |
| 77 | entry.status === "warning" && |
| 78 | entry.installed && |
| 79 | auth.status === "authenticated" && |
| 80 | entry.models.length > 0 |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | export function isProviderInstancePickerVisible(entry: ProviderInstanceEntry): boolean { |
| 85 | return entry.enabled; |
| 86 | } |
| 87 | |
| 88 | export function deriveProviderInstanceEntries( |
| 89 | providers: ReadonlyArray<ServerProvider>, |
| 90 | ): ReadonlyArray<ProviderInstanceEntry> { |
| 91 | return providers.map((snapshot) => { |
| 92 | const instanceId = snapshot.instanceId; |
| 93 | const driverKind = snapshot.driver; |
| 94 | const defaultId = defaultInstanceIdForDriver(driverKind); |
| 95 | const isDefault = instanceId === defaultId; |
| 96 | return { |
| 97 | instanceId, |
| 98 | driverKind, |
| 99 | displayName: resolveProviderInstanceDisplayName(snapshot), |
| 100 | accentColor: normalizeProviderAccentColor(snapshot.accentColor), |
| 101 | ...(driverKind === "acpRegistry" && snapshot.iconUrl |
| 102 | ? { acpRegistryIconUrl: snapshot.iconUrl } |
| 103 | : {}), |
| 104 | continuationGroupKey: snapshot.continuation?.groupKey, |
| 105 | enabled: snapshot.enabled, |
| 106 | installed: snapshot.installed, |
| 107 | status: snapshot.status, |
| 108 | isDefault, |
| 109 | isAvailable: snapshot.availability !== "unavailable", |
| 110 | snapshot, |
| 111 | models: snapshot.models, |
| 112 | } satisfies ProviderInstanceEntry; |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | /** Instance ids repeat across environments; lookups must stay scoped. */ |
| 117 | export function deriveProviderEntriesByEnvironment( |
| 118 | providersByEnvironment: Iterable< |
| 119 | readonly [ |
| 120 | string, |
| 121 | ReadonlyArray<ServerProvider>, |
| 122 | Pick<ServerSettings, "providerInstances" | "projectSettingsOverrides">?, |
| 123 | ] |
| 124 | >, |
| 125 | ): ReadonlyMap<string, ReadonlyMap<string, ProviderInstanceEntry>> { |
| 126 | const byEnvironment = new Map<string, ReadonlyMap<string, ProviderInstanceEntry>>(); |
| 127 | for (const [environmentId, providers, settings] of providersByEnvironment) { |
| 128 | const derived = deriveProviderInstanceEntries(providers); |
| 129 | const entries = settings ? brandProviderInstanceEntries(derived, settings) : derived; |
| 130 | byEnvironment.set( |
| 131 | environmentId, |
| 132 | new Map(entries.map((entry) => [entry.instanceId as string, entry] as const)), |
| 133 | ); |
| 134 | } |
| 135 | return byEnvironment; |
| 136 | } |
| 137 | |
| 138 | // The snapshot includes instances enabled by any project; machine settings alone do not. |
| 139 | export function brandProviderInstanceEntries( |
| 140 | entries: ReadonlyArray<ProviderInstanceEntry>, |
| 141 | settings: Pick<ServerSettings, "providerInstances">, |
| 142 | ): ReadonlyArray<ProviderInstanceEntry> { |
| 143 | return entries.map((entry) => applyProviderInstanceBranding(entry, settings)); |
| 144 | } |
| 145 | |
| 146 | /** projectId applies project overrides; explicit null uses machine settings. */ |
| 147 | export function applyProviderInstanceSettings( |
| 148 | entries: ReadonlyArray<ProviderInstanceEntry>, |
| 149 | settings: Pick<ServerSettings, "providerInstances" | "projectSettingsOverrides">, |
| 150 | projectId: ProjectId | null, |
| 151 | ): ReadonlyArray<ProviderInstanceEntry> { |
| 152 | return entries.map((entry) => { |
| 153 | const enabled = resolveProjectProviderInstanceEnabled(settings, projectId, entry.instanceId); |
| 154 | return applyProviderInstanceBranding( |
| 155 | enabled === entry.enabled ? entry : { ...entry, enabled }, |
| 156 | settings, |
| 157 | ); |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | function applyProviderInstanceBranding( |
| 162 | entry: ProviderInstanceEntry, |
| 163 | settings: Pick<ServerSettings, "providerInstances">, |
| 164 | ): ProviderInstanceEntry { |
| 165 | const explicitInstance = Object.hasOwn(settings.providerInstances, entry.instanceId) |
| 166 | ? settings.providerInstances[entry.instanceId] |
| 167 | : undefined; |
| 168 | if (entry.driverKind !== "acpRegistry" || explicitInstance === undefined) return entry; |
| 169 | const config = |
| 170 | explicitInstance.config !== null && typeof explicitInstance.config === "object" |
| 171 | ? (explicitInstance.config as Readonly<Record<string, unknown>>) |
| 172 | : null; |
| 173 | if (config?.source === "local") { |
| 174 | return { ...entry, acpRegistryAgentId: undefined, acpRegistryIconUrl: undefined }; |
| 175 | } |
| 176 | const agentId = config?.agentId; |
| 177 | const iconUrl = config?.registryIconUrl; |
| 178 | return { |
| 179 | ...entry, |
| 180 | ...(typeof agentId === "string" && agentId.trim() |
| 181 | ? { acpRegistryAgentId: agentId.trim() } |
| 182 | : {}), |
| 183 | ...(typeof iconUrl === "string" && iconUrl.trim() |
| 184 | ? { acpRegistryIconUrl: iconUrl.trim() } |
| 185 | : {}), |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | /** Picker order: each driver's default instance first, then its custom instances. */ |
| 190 | export function providerInstanceEntriesFor( |
| 191 | providers: ReadonlyArray<ServerProvider>, |
| 192 | settings: Pick<ServerSettings, "providerInstances" | "projectSettingsOverrides">, |
| 193 | projectId: ProjectId | null, |
| 194 | ): ReadonlyArray<ProviderInstanceEntry> { |
| 195 | return pipe( |
| 196 | deriveProviderInstanceEntries(providers), |
| 197 | (entries) => applyProviderInstanceSettings(entries, settings, projectId), |
| 198 | sortProviderInstanceEntries, |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | function sortProviderInstanceEntries( |
| 203 | entries: ReadonlyArray<ProviderInstanceEntry>, |
| 204 | ): ReadonlyArray<ProviderInstanceEntry> { |
| 205 | const byKind = new Map<ProviderDriverKind, ProviderInstanceEntry[]>(); |
| 206 | for (const entry of entries) { |
| 207 | const bucket = byKind.get(entry.driverKind); |
| 208 | if (bucket) { |
| 209 | bucket.push(entry); |
| 210 | } else { |
| 211 | byKind.set(entry.driverKind, [entry]); |
| 212 | } |
| 213 | } |
| 214 | const sorted: ProviderInstanceEntry[] = []; |
| 215 | for (const bucket of byKind.values()) { |
| 216 | const defaults = bucket.filter((entry) => entry.isDefault); |
| 217 | const customs = bucket.filter((entry) => !entry.isDefault); |
| 218 | sorted.push(...defaults, ...customs); |
| 219 | } |
| 220 | return sorted; |
| 221 | } |
| 222 | |
| 223 | function getProviderInstanceEntry( |
| 224 | providers: ReadonlyArray<ServerProvider>, |
| 225 | instanceId: ProviderInstanceId, |
| 226 | ): ProviderInstanceEntry | undefined { |
| 227 | return deriveProviderInstanceEntries(providers).find((entry) => entry.instanceId === instanceId); |
| 228 | } |
| 229 | |
| 230 | export function getDefaultProviderInstanceModel( |
| 231 | providers: ReadonlyArray<ServerProvider>, |
| 232 | instanceId: ProviderInstanceId, |
| 233 | ): string | undefined { |
| 234 | const entry = getProviderInstanceEntry(providers, instanceId); |
| 235 | if (!entry) return undefined; |
| 236 | return ( |
| 237 | entry.models.find((model) => model.isDefault && !model.isCustom)?.slug ?? |
| 238 | entry.models.find((model) => !model.isCustom)?.slug ?? |
| 239 | entry.models[0]?.slug ?? |
| 240 | DEFAULT_MODEL_BY_PROVIDER[entry.driverKind] |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | const isSelectableProviderInstanceEntry = (entry: ProviderInstanceEntry): boolean => |
| 245 | entry.enabled && entry.isAvailable; |
| 246 | |
| 247 | export function resolveSelectableProviderInstanceEntry( |
| 248 | entries: ReadonlyArray<ProviderInstanceEntry>, |
| 249 | instanceId: ProviderInstanceId | undefined, |
| 250 | ): ProviderInstanceEntry | undefined { |
| 251 | if (instanceId !== undefined) { |
| 252 | const requested = entries.find((entry) => entry.instanceId === instanceId); |
| 253 | if (requested && isSelectableProviderInstanceEntry(requested)) { |
| 254 | return requested; |
| 255 | } |
| 256 | } |
| 257 | return ( |
| 258 | entries.find(isProviderInstanceHealthy) ?? |
| 259 | entries.find((entry) => isSelectableProviderInstanceEntry(entry) && entry.status !== "error") |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | export function resolveSelectableProviderInstance( |
| 264 | providers: ReadonlyArray<ServerProvider>, |
| 265 | instanceId: ProviderInstanceId | undefined, |
| 266 | ): ProviderInstanceId | undefined { |
| 267 | const entries = deriveProviderInstanceEntries(providers); |
| 268 | return resolveSelectableProviderInstanceEntry(entries, instanceId)?.instanceId; |
| 269 | } |
| 270 | |
| 271 | export function resolveDefaultProviderModelSelection( |
| 272 | providers: ReadonlyArray<ServerProvider>, |
| 273 | selection: ModelSelection | null | undefined, |
| 274 | ): ModelSelection | null { |
| 275 | const instanceId = resolveSelectableProviderInstance(providers, selection?.instanceId); |
| 276 | if (instanceId === undefined) return null; |
| 277 | if (selection?.instanceId === instanceId) return selection; |
| 278 | const model = getDefaultProviderInstanceModel(providers, instanceId); |
| 279 | return model ? { instanceId, model } : null; |
| 280 | } |
| 281 | |
| 282 | export function resolveProjectDefaultProviderModelSelection( |
| 283 | providers: ReadonlyArray<ServerProvider>, |
| 284 | settings: ServerSettings, |
| 285 | projectId: ProjectId | null, |
| 286 | ): ModelSelection | null { |
| 287 | const entries = providerInstanceEntriesFor(providers, settings, projectId); |
| 288 | // Matches resolveComposerProviderSelection: the first driver group wins before any other. |
| 289 | const entry = |
| 290 | resolveSelectableProviderInstanceEntry( |
| 291 | entries.filter((candidate) => candidate.driverKind === entries[0]?.driverKind), |
| 292 | undefined, |
| 293 | ) ?? resolveSelectableProviderInstanceEntry(entries, undefined); |
| 294 | const model = entry ? getDefaultProviderInstanceModel(providers, entry.instanceId) : undefined; |
| 295 | return entry && model ? { instanceId: entry.instanceId, model } : null; |
| 296 | } |