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