source dump of claude code
at main 77 lines 2.7 kB view raw
1import { SETTING_SOURCES, type SettingSource } from '../settings/constants.js' 2import { 3 getSettings_DEPRECATED, 4 getSettingsForSource, 5} from '../settings/settings.js' 6import { type EnvironmentResource, fetchEnvironments } from './environments.js' 7 8export type EnvironmentSelectionInfo = { 9 availableEnvironments: EnvironmentResource[] 10 selectedEnvironment: EnvironmentResource | null 11 selectedEnvironmentSource: SettingSource | null 12} 13 14/** 15 * Gets information about available environments and the currently selected one. 16 * 17 * @returns Promise<EnvironmentSelectionInfo> containing: 18 * - availableEnvironments: all environments from the API 19 * - selectedEnvironment: the environment that would be used (based on settings or first available), 20 * or null if no environments are available 21 * - selectedEnvironmentSource: the SettingSource where defaultEnvironmentId is configured, 22 * or null if using the default (first environment) 23 */ 24export async function getEnvironmentSelectionInfo(): Promise<EnvironmentSelectionInfo> { 25 // Fetch available environments 26 const environments = await fetchEnvironments() 27 28 if (environments.length === 0) { 29 return { 30 availableEnvironments: [], 31 selectedEnvironment: null, 32 selectedEnvironmentSource: null, 33 } 34 } 35 36 // Get the merged settings to see what would actually be used 37 const mergedSettings = getSettings_DEPRECATED() 38 const defaultEnvironmentId = mergedSettings?.remote?.defaultEnvironmentId 39 40 // Find which environment would be selected 41 let selectedEnvironment: EnvironmentResource = 42 environments.find(env => env.kind !== 'bridge') ?? environments[0]! 43 let selectedEnvironmentSource: SettingSource | null = null 44 45 if (defaultEnvironmentId) { 46 const matchingEnvironment = environments.find( 47 env => env.environment_id === defaultEnvironmentId, 48 ) 49 50 if (matchingEnvironment) { 51 selectedEnvironment = matchingEnvironment 52 53 // Find which source has this setting 54 // Iterate from lowest to highest priority, so the last match wins (highest priority) 55 for (let i = SETTING_SOURCES.length - 1; i >= 0; i--) { 56 const source = SETTING_SOURCES[i] 57 if (!source || source === 'flagSettings') { 58 // Skip flagSettings as it's not a normal source we check 59 continue 60 } 61 const sourceSettings = getSettingsForSource(source) 62 if ( 63 sourceSettings?.remote?.defaultEnvironmentId === defaultEnvironmentId 64 ) { 65 selectedEnvironmentSource = source 66 break 67 } 68 } 69 } 70 } 71 72 return { 73 availableEnvironments: environments, 74 selectedEnvironment, 75 selectedEnvironmentSource, 76 } 77}