The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | normalizeProviderAccentColor, |
| 3 | resolveProviderInstanceDisplayName, |
| 4 | shouldShowInstanceBadge, |
| 5 | } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 6 | import { msg } from "gt-react-native"; |
| 7 | import { m } from "../i18n/messages"; |
| 8 | |
| 9 | import type { |
| 10 | ModelCapabilities, |
| 11 | ModelSelection, |
| 12 | ProjectId, |
| 13 | RuntimeMode, |
| 14 | ServerConfig as T3ServerConfig, |
| 15 | } from "@t3tools/contracts"; |
| 16 | import { |
| 17 | isManagedDriverAllowed, |
| 18 | isProviderDriverKind, |
| 19 | resolveProjectProviderInstanceEnabled, |
| 20 | } from "@t3tools/contracts"; |
| 21 | import { |
| 22 | buildExplicitProviderOptionSelectionsFromDescriptors, |
| 23 | getProviderOptionDescriptors, |
| 24 | } from "@t3tools/shared/model"; |
| 25 | import { resolveModelShortName } from "@t3tools/client-runtime/provider-clients"; |
| 26 | import { resolveProvidersForCwd } from "@t3tools/client-runtime/providerSkills"; |
| 27 | |
| 28 | /** The contracts `disallowedProviderMessage` English, as translatable sentences. */ |
| 29 | function disallowedProviderText(organization: string | undefined, label: string): string { |
| 30 | return organization === undefined |
| 31 | ? m(msg("Your organization policy does not allow the {label} provider.", { label })) |
| 32 | : m( |
| 33 | msg("{organization}''s policy does not allow the {label} provider.", { |
| 34 | organization, |
| 35 | label, |
| 36 | }), |
| 37 | ); |
| 38 | } |
| 39 | export type ModelOption = { |
| 40 | readonly key: string; |
| 41 | readonly label: string; |
| 42 | readonly shortName?: string; |
| 43 | readonly subtitle: string; |
| 44 | readonly providerKey: string; |
| 45 | readonly providerLabel: string; |
| 46 | readonly providerDriver: string; |
| 47 | readonly supportedRuntimeModes?: ReadonlyArray<RuntimeMode>; |
| 48 | readonly providerIconUrl?: string | undefined; |
| 49 | readonly providerBadge?: { |
| 50 | readonly displayName: string; |
| 51 | readonly accentColor?: string; |
| 52 | readonly showBadge: boolean; |
| 53 | }; |
| 54 | readonly isDefault: boolean; |
| 55 | readonly isLegacy: boolean; |
| 56 | readonly isUnavailable?: boolean; |
| 57 | readonly isHidden?: boolean; |
| 58 | readonly canHide?: boolean; |
| 59 | readonly capabilities: ModelCapabilities | null; |
| 60 | readonly selection: ModelSelection; |
| 61 | readonly providerUpdateRequired?: ProviderUpdateRequired; |
| 62 | readonly providerSubProviders?: T3ServerConfig["providers"][number]["subProviders"]; |
| 63 | }; |
| 64 | |
| 65 | type ProviderUpdateRequired = Pick< |
| 66 | T3ServerConfig["providers"][number], |
| 67 | "driver" | "updateRequiredModels" |
| 68 | >; |
| 69 | |
| 70 | export type ProviderGroup = { |
| 71 | readonly providerKey: string; |
| 72 | readonly providerLabel: string; |
| 73 | readonly models: ReadonlyArray<ModelOption>; |
| 74 | readonly updateRequired?: ProviderUpdateRequired; |
| 75 | readonly subProviders?: ModelOption["providerSubProviders"]; |
| 76 | }; |
| 77 | |
| 78 | function providerDisplayLabel(provider: { |
| 79 | readonly displayName?: string | undefined; |
| 80 | readonly driver: string; |
| 81 | readonly instanceId: string; |
| 82 | }): string { |
| 83 | if (provider.displayName) return provider.displayName; |
| 84 | if (provider.driver === "codex") return "Codex"; |
| 85 | if (provider.driver === "claudeAgent") return "Claude"; |
| 86 | if (provider.driver === "pi") return "Pi"; |
| 87 | return provider.instanceId; |
| 88 | } |
| 89 | |
| 90 | function isProviderEnabledForProject( |
| 91 | config: T3ServerConfig | null | undefined, |
| 92 | provider: |
| 93 | | { readonly instanceId: ModelSelection["instanceId"]; readonly enabled: boolean } |
| 94 | | undefined, |
| 95 | projectId: ProjectId | null, |
| 96 | ): boolean { |
| 97 | if (!provider) { |
| 98 | return false; |
| 99 | } |
| 100 | return config?.settings |
| 101 | ? resolveProjectProviderInstanceEnabled(config.settings, projectId, provider.instanceId) |
| 102 | : provider.enabled; |
| 103 | } |
| 104 | |
| 105 | function isProjectDisabledOverride( |
| 106 | config: T3ServerConfig | null | undefined, |
| 107 | instanceId: ModelSelection["instanceId"], |
| 108 | projectId: ProjectId | null, |
| 109 | ): boolean { |
| 110 | if (!projectId || !config?.settings) { |
| 111 | return false; |
| 112 | } |
| 113 | return ( |
| 114 | config.settings.projectSettingsOverrides[projectId]?.providerInstances?.[instanceId] |
| 115 | ?.enabled === false |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | function isDisabledForProject( |
| 120 | config: T3ServerConfig | null | undefined, |
| 121 | provider: |
| 122 | | { readonly instanceId: ModelSelection["instanceId"]; readonly enabled: boolean } |
| 123 | | undefined, |
| 124 | instanceId: ModelSelection["instanceId"], |
| 125 | projectId: ProjectId | null, |
| 126 | ): boolean { |
| 127 | if (isProjectDisabledOverride(config, instanceId, projectId)) { |
| 128 | return true; |
| 129 | } |
| 130 | return provider !== undefined && !isProviderEnabledForProject(config, provider, projectId); |
| 131 | } |
| 132 | |
| 133 | function selectionDriver( |
| 134 | config: T3ServerConfig | null | undefined, |
| 135 | instanceId: ModelSelection["instanceId"], |
| 136 | ): string | undefined { |
| 137 | return ( |
| 138 | config?.providers.find((candidate) => candidate.instanceId === instanceId)?.driver ?? |
| 139 | config?.settings?.providerInstances[instanceId]?.driver |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** Whether the environment's managed policy leaves this driver out; the server refuses its turns. */ |
| 144 | function isDriverDisallowed(config: T3ServerConfig | null | undefined, driver: string | undefined) { |
| 145 | return ( |
| 146 | driver !== undefined && |
| 147 | isProviderDriverKind(driver) && |
| 148 | !isManagedDriverAllowed(config?.managedPolicy, driver) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | function normalizeSelectionOptions( |
| 153 | selection: ModelSelection, |
| 154 | capabilities: ModelCapabilities | null, |
| 155 | ): ModelSelection { |
| 156 | if (!capabilities) { |
| 157 | return selection; |
| 158 | } |
| 159 | if (!selection.options?.length) { |
| 160 | return { instanceId: selection.instanceId, model: selection.model }; |
| 161 | } |
| 162 | const options = buildExplicitProviderOptionSelectionsFromDescriptors( |
| 163 | getProviderOptionDescriptors({ |
| 164 | caps: capabilities, |
| 165 | selections: selection.options, |
| 166 | }), |
| 167 | selection.options, |
| 168 | ); |
| 169 | return options |
| 170 | ? { ...selection, options } |
| 171 | : { |
| 172 | instanceId: selection.instanceId, |
| 173 | model: selection.model, |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | /** The config as a project at `cwd` sees it, with that project's own provider models. */ |
| 178 | export function serverConfigForCwd<C extends T3ServerConfig | null | undefined>( |
| 179 | config: C, |
| 180 | cwd: string | null, |
| 181 | ): C { |
| 182 | if (!config) return config; |
| 183 | const providers = resolveProvidersForCwd(config.providers, cwd); |
| 184 | return providers === config.providers ? config : { ...config, providers }; |
| 185 | } |
| 186 | |
| 187 | /** Require explicit projectId or null so omissions cannot bypass project provider disablement. */ |
| 188 | export function isModelSelectionUnavailable( |
| 189 | config: T3ServerConfig | null | undefined, |
| 190 | selection: ModelSelection | null | undefined, |
| 191 | projectId: ProjectId | null, |
| 192 | ): boolean { |
| 193 | if (!config || !selection) { |
| 194 | return false; |
| 195 | } |
| 196 | const provider = config.providers.find( |
| 197 | (candidate) => candidate.instanceId === selection.instanceId, |
| 198 | ); |
| 199 | if (isDisabledForProject(config, provider, selection.instanceId, projectId)) { |
| 200 | return true; |
| 201 | } |
| 202 | const driver = |
| 203 | provider?.driver ?? config.settings?.providerInstances[selection.instanceId]?.driver; |
| 204 | if (isDriverDisallowed(config, driver)) { |
| 205 | return true; |
| 206 | } |
| 207 | return ( |
| 208 | driver === "antigravity" && |
| 209 | (!provider || |
| 210 | !provider.installed || |
| 211 | provider.auth.status === "unauthenticated" || |
| 212 | provider.availability === "unavailable" || |
| 213 | !provider.models.some((model) => model.slug === selection.model)) |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | export function describeUnavailableModelSelection( |
| 218 | config: T3ServerConfig | null | undefined, |
| 219 | selection: ModelSelection, |
| 220 | projectId: ProjectId | null, |
| 221 | ): { readonly title: string; readonly detail: string } { |
| 222 | const provider = config?.providers.find( |
| 223 | (candidate) => candidate.instanceId === selection.instanceId, |
| 224 | ); |
| 225 | const driver = selectionDriver(config, selection.instanceId); |
| 226 | if (isDriverDisallowed(config, driver)) { |
| 227 | const label = providerDisplayLabel({ |
| 228 | driver: driver ?? selection.instanceId, |
| 229 | displayName: |
| 230 | provider?.displayName ?? |
| 231 | config?.settings?.providerInstances[selection.instanceId]?.displayName, |
| 232 | instanceId: selection.instanceId, |
| 233 | }); |
| 234 | return { |
| 235 | title: m(msg("Provider not allowed")), |
| 236 | detail: m( |
| 237 | msg("{message} Choose another model.", { |
| 238 | message: disallowedProviderText(config?.managedPolicy?.organization, label), |
| 239 | }), |
| 240 | ), |
| 241 | }; |
| 242 | } |
| 243 | return isDisabledForProject(config, provider, selection.instanceId, projectId) |
| 244 | ? { |
| 245 | title: m(msg("Model turned off for this project")), |
| 246 | detail: m(msg("Turn it back on from web or desktop, or choose another model.")), |
| 247 | } |
| 248 | : { |
| 249 | title: m(msg("{provider} model unavailable", { provider: "Antigravity" })), |
| 250 | detail: m( |
| 251 | msg("Set up {provider} on web or desktop, or choose another model.", { |
| 252 | provider: "Antigravity", |
| 253 | }), |
| 254 | ), |
| 255 | }; |
| 256 | } |
| 257 | |
| 258 | export function resolveSelectableModelSelection( |
| 259 | config: T3ServerConfig | null | undefined, |
| 260 | selection: ModelSelection | null, |
| 261 | projectId: ProjectId | null, |
| 262 | ): ModelSelection | null { |
| 263 | if (!selection || !config) { |
| 264 | return selection; |
| 265 | } |
| 266 | const provider = config.providers.find( |
| 267 | (candidate) => candidate.instanceId === selection.instanceId, |
| 268 | ); |
| 269 | const driver = |
| 270 | provider?.driver ?? config.settings?.providerInstances[selection.instanceId]?.driver; |
| 271 | if (driver === "antigravity") { |
| 272 | return isProjectDisabledOverride(config, selection.instanceId, projectId) ? null : selection; |
| 273 | } |
| 274 | return provider && |
| 275 | isProviderEnabledForProject(config, provider, projectId) && |
| 276 | provider.installed && |
| 277 | provider.auth.status !== "unauthenticated" |
| 278 | ? selection |
| 279 | : null; |
| 280 | } |
| 281 | |
| 282 | export function resolveDefaultableModelSelection( |
| 283 | config: T3ServerConfig | null | undefined, |
| 284 | selection: ModelSelection | null, |
| 285 | projectId: ProjectId | null, |
| 286 | ): ModelSelection | null { |
| 287 | const usable = resolveSelectableModelSelection(config, selection, projectId); |
| 288 | if (!usable || !config) { |
| 289 | return usable; |
| 290 | } |
| 291 | const provider = config.providers.find((candidate) => candidate.instanceId === usable.instanceId); |
| 292 | const model = provider?.models.find((candidate) => candidate.slug === usable.model); |
| 293 | return provider?.driver !== "antigravity" && model?.isLegacy === true ? null : usable; |
| 294 | } |
| 295 | |
| 296 | export function resolveNewTaskModelSelection(input: { |
| 297 | readonly draftSelection: ModelSelection | null; |
| 298 | readonly projectDefaultSelection: ModelSelection | null; |
| 299 | readonly stickySelection: ModelSelection | null; |
| 300 | readonly modelOptions: ReadonlyArray<ModelOption>; |
| 301 | }): ModelSelection | null { |
| 302 | return ( |
| 303 | input.draftSelection ?? |
| 304 | input.projectDefaultSelection ?? |
| 305 | input.stickySelection ?? |
| 306 | input.modelOptions.find((option) => option.isDefault && !option.isUnavailable)?.selection ?? |
| 307 | input.modelOptions.find((option) => !option.isUnavailable)?.selection ?? |
| 308 | null |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | // Rows built after a pick on another instance carry no options for the thread's own model. |
| 313 | export function withStoredThreadModelOptions( |
| 314 | selection: ModelSelection, |
| 315 | stored: ModelSelection, |
| 316 | ): ModelSelection { |
| 317 | if ( |
| 318 | (selection.options?.length ?? 0) > 0 || |
| 319 | !stored.options?.length || |
| 320 | selection.instanceId !== stored.instanceId || |
| 321 | selection.model !== stored.model |
| 322 | ) { |
| 323 | return selection; |
| 324 | } |
| 325 | return { ...selection, options: stored.options }; |
| 326 | } |
| 327 | |
| 328 | export function buildModelOptions( |
| 329 | config: T3ServerConfig | null | undefined, |
| 330 | fallbackModelSelection: ModelSelection | null, |
| 331 | providerInstanceId: ModelSelection["instanceId"] | undefined, |
| 332 | projectId: ProjectId | null, |
| 333 | includeHidden = false, |
| 334 | ): ReadonlyArray<ModelOption> { |
| 335 | const options = new Map<string, ModelOption>(); |
| 336 | const lockedProvider = config?.providers.find( |
| 337 | (provider) => provider.instanceId === providerInstanceId, |
| 338 | ); |
| 339 | const matchesLockedProvider = (provider: T3ServerConfig["providers"][number]) => { |
| 340 | if (providerInstanceId === undefined || provider.instanceId === providerInstanceId) return true; |
| 341 | if (!lockedProvider || provider.driver !== lockedProvider.driver) return false; |
| 342 | const groupKey = lockedProvider.continuation?.groupKey; |
| 343 | if (groupKey) return provider.continuation?.groupKey === groupKey; |
| 344 | return provider.driver !== "antigravity"; |
| 345 | }; |
| 346 | const registryAgentId = (instanceId: ModelSelection["instanceId"]) => { |
| 347 | const configValue = config?.settings?.providerInstances[instanceId]?.config; |
| 348 | if (configValue === null || typeof configValue !== "object") return undefined; |
| 349 | const agentId = "agentId" in configValue ? configValue.agentId : undefined; |
| 350 | return "source" in configValue && configValue.source === "local" |
| 351 | ? undefined |
| 352 | : typeof agentId === "string" |
| 353 | ? agentId.trim() || undefined |
| 354 | : undefined; |
| 355 | }; |
| 356 | const badgeEntries = (config?.providers ?? []).map((provider) => ({ |
| 357 | driverKind: provider.driver, |
| 358 | enabled: provider.enabled, |
| 359 | acpRegistryAgentId: registryAgentId(provider.instanceId), |
| 360 | })); |
| 361 | const providerBadgeFor = (instanceId: ModelSelection["instanceId"]) => { |
| 362 | const snapshot = config?.providers.find((provider) => provider.instanceId === instanceId); |
| 363 | const instance = config?.settings?.providerInstances[instanceId]; |
| 364 | const driver = snapshot?.driver ?? instance?.driver; |
| 365 | if (!driver) return undefined; |
| 366 | const accentColor = normalizeProviderAccentColor( |
| 367 | snapshot?.accentColor ?? instance?.accentColor, |
| 368 | ); |
| 369 | return { |
| 370 | displayName: resolveProviderInstanceDisplayName({ |
| 371 | instanceId, |
| 372 | driver, |
| 373 | displayName: snapshot?.displayName ?? instance?.displayName, |
| 374 | }), |
| 375 | ...(accentColor ? { accentColor } : {}), |
| 376 | showBadge: shouldShowInstanceBadge( |
| 377 | { |
| 378 | driverKind: driver, |
| 379 | accentColor, |
| 380 | acpRegistryAgentId: registryAgentId(instanceId), |
| 381 | enabled: snapshot?.enabled ?? instance?.enabled ?? false, |
| 382 | }, |
| 383 | badgeEntries, |
| 384 | ), |
| 385 | }; |
| 386 | }; |
| 387 | |
| 388 | for (const provider of config?.providers ?? []) { |
| 389 | if ( |
| 390 | !matchesLockedProvider(provider) || |
| 391 | isDriverDisallowed(config, provider.driver) || |
| 392 | !isProviderEnabledForProject(config, provider, projectId) || |
| 393 | !provider.installed || |
| 394 | provider.auth.status === "unauthenticated" || |
| 395 | (provider.driver === "antigravity" && provider.availability === "unavailable") |
| 396 | ) { |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | const providerLabel = providerDisplayLabel(provider); |
| 401 | const providerBadge = providerBadgeFor(provider.instanceId); |
| 402 | const updateRequired = provider.updateRequiredModels?.length |
| 403 | ? { driver: provider.driver, updateRequiredModels: provider.updateRequiredModels } |
| 404 | : undefined; |
| 405 | const preferences = config?.settings?.providerModelPreferences?.[provider.instanceId]; |
| 406 | const hiddenModels = new Set(preferences?.hiddenModels ?? []); |
| 407 | const order = new Map((preferences?.modelOrder ?? []).map((slug, index) => [slug, index])); |
| 408 | const models = provider.models.filter( |
| 409 | (model) => includeHidden || model.isCustom || !hiddenModels.has(model.slug), |
| 410 | ); |
| 411 | if (order.size > 0) |
| 412 | models.sort((a, b) => (order.get(a.slug) ?? Infinity) - (order.get(b.slug) ?? Infinity)); |
| 413 | for (const model of models) { |
| 414 | const key = `${provider.instanceId}:${model.slug}`; |
| 415 | const shortName = resolveModelShortName(model, provider.driver); |
| 416 | options.set(key, { |
| 417 | key, |
| 418 | label: model.name, |
| 419 | ...(shortName ? { shortName } : {}), |
| 420 | subtitle: model.subProvider ?? "", |
| 421 | providerKey: provider.instanceId, |
| 422 | providerLabel, |
| 423 | providerDriver: provider.driver, |
| 424 | providerBadge, |
| 425 | ...(provider.supportedRuntimeModes === undefined |
| 426 | ? {} |
| 427 | : { supportedRuntimeModes: provider.supportedRuntimeModes }), |
| 428 | ...(provider.iconUrl ? { providerIconUrl: provider.iconUrl } : {}), |
| 429 | isHidden: !model.isCustom && hiddenModels.has(model.slug), |
| 430 | canHide: !model.isCustom, |
| 431 | isDefault: model.isDefault === true, |
| 432 | isLegacy: model.isLegacy === true, |
| 433 | ...(updateRequired ? { providerUpdateRequired: updateRequired } : {}), |
| 434 | ...(provider.subProviders?.length ? { providerSubProviders: provider.subProviders } : {}), |
| 435 | capabilities: model.capabilities, |
| 436 | selection: normalizeSelectionOptions( |
| 437 | { |
| 438 | instanceId: provider.instanceId, |
| 439 | model: model.slug, |
| 440 | }, |
| 441 | model.capabilities, |
| 442 | ), |
| 443 | }); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if ( |
| 448 | fallbackModelSelection && |
| 449 | (providerInstanceId === undefined || |
| 450 | fallbackModelSelection.instanceId === providerInstanceId) && |
| 451 | (!config?.settings?.providerModelPreferences?.[ |
| 452 | fallbackModelSelection.instanceId |
| 453 | ]?.hiddenModels.includes(fallbackModelSelection.model) || |
| 454 | config?.providers |
| 455 | .find((provider) => provider.instanceId === fallbackModelSelection.instanceId) |
| 456 | ?.models.find((model) => model.slug === fallbackModelSelection.model)?.isCustom) |
| 457 | ) { |
| 458 | const key = `${fallbackModelSelection.instanceId}:${fallbackModelSelection.model}`; |
| 459 | const existing = options.get(key); |
| 460 | if (existing) { |
| 461 | options.set(key, { |
| 462 | ...existing, |
| 463 | selection: |
| 464 | existing.providerDriver === "antigravity" |
| 465 | ? fallbackModelSelection |
| 466 | : normalizeSelectionOptions(fallbackModelSelection, existing.capabilities), |
| 467 | }); |
| 468 | } else { |
| 469 | const provider = config?.providers.find( |
| 470 | (candidate) => candidate.instanceId === fallbackModelSelection.instanceId, |
| 471 | ); |
| 472 | const instanceConfig = config?.settings?.providerInstances[fallbackModelSelection.instanceId]; |
| 473 | const model = provider?.models.find( |
| 474 | (candidate) => candidate.slug === fallbackModelSelection.model, |
| 475 | ); |
| 476 | const providerDriver = |
| 477 | provider?.driver ?? instanceConfig?.driver ?? fallbackModelSelection.instanceId; |
| 478 | const providerLabel = providerDisplayLabel({ |
| 479 | driver: providerDriver, |
| 480 | displayName: provider?.displayName ?? instanceConfig?.displayName, |
| 481 | instanceId: fallbackModelSelection.instanceId, |
| 482 | }); |
| 483 | const shortName = model |
| 484 | ? resolveModelShortName(model, isProviderDriverKind(providerDriver) ? providerDriver : null) |
| 485 | : undefined; |
| 486 | options.set(key, { |
| 487 | key, |
| 488 | label: model?.name ?? fallbackModelSelection.model, |
| 489 | ...(shortName ? { shortName } : {}), |
| 490 | subtitle: model?.subProvider ?? "", |
| 491 | providerKey: fallbackModelSelection.instanceId, |
| 492 | providerLabel, |
| 493 | providerDriver, |
| 494 | providerBadge: providerBadgeFor(fallbackModelSelection.instanceId), |
| 495 | isDefault: false, |
| 496 | isLegacy: model?.isLegacy === true, |
| 497 | ...(isModelSelectionUnavailable(config, fallbackModelSelection, projectId) || |
| 498 | (provider !== undefined && !isProviderEnabledForProject(config, provider, projectId)) || |
| 499 | provider?.updateRequiredModels?.some((gated) => gated.slug === fallbackModelSelection.model) |
| 500 | ? { isUnavailable: true } |
| 501 | : {}), |
| 502 | capabilities: model?.capabilities ?? null, |
| 503 | selection: fallbackModelSelection, |
| 504 | }); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | return [...options.values()]; |
| 509 | } |
| 510 | |
| 511 | export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyArray<ProviderGroup> { |
| 512 | const groups = new Map< |
| 513 | string, |
| 514 | { |
| 515 | providerLabel: string; |
| 516 | models: ModelOption[]; |
| 517 | updateRequired: ProviderUpdateRequired | undefined; |
| 518 | subProviders: ModelOption["providerSubProviders"]; |
| 519 | } |
| 520 | >(); |
| 521 | for (const option of options) { |
| 522 | const existing = groups.get(option.providerKey); |
| 523 | if (existing) { |
| 524 | existing.models.push(option); |
| 525 | existing.updateRequired ??= option.providerUpdateRequired; |
| 526 | existing.subProviders ??= option.providerSubProviders; |
| 527 | } else { |
| 528 | groups.set(option.providerKey, { |
| 529 | providerLabel: option.providerLabel, |
| 530 | models: [option], |
| 531 | updateRequired: option.providerUpdateRequired, |
| 532 | subProviders: option.providerSubProviders, |
| 533 | }); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return [...groups.entries()].map(([providerKey, group]) => ({ |
| 538 | providerKey, |
| 539 | providerLabel: group.providerLabel, |
| 540 | models: group.models, |
| 541 | ...(group.updateRequired ? { updateRequired: group.updateRequired } : {}), |
| 542 | ...(group.subProviders ? { subProviders: group.subProviders } : {}), |
| 543 | })); |
| 544 | } |