-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcapability-setup.ts
More file actions
558 lines (515 loc) · 19.6 KB
/
Copy pathcapability-setup.ts
File metadata and controls
558 lines (515 loc) · 19.6 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* Generic prompt rendering and environment transitions for the CLI setup catalog.
*
* @packageDocumentation
*/
import {
EnvCapabilityConfigurationError,
type EnvCapabilityValue,
type EnvCapabilityValues,
getProviderFields,
hasEnvCapabilityValue,
inspectCapability,
isTruthyEnvCapabilityValue,
validateCapabilityFieldInput,
} from '../../apps/sim/lib/core/config/env-capabilities.ts'
import {
type CapabilitySetupDefinition,
matchesSetupCondition,
type SetupCondition,
type SetupHint,
type SetupPrompt,
} from './capability-config.ts'
import * as p from './prompter.ts'
export interface CapabilitySetupContext {
containerized: boolean
}
export interface EnvCapabilitySetupTransition {
values: Record<string, string>
remove: readonly string[]
}
interface ResolvedSetupOption {
id: string
label: string
hint?: SetupHint
kind: 'provider' | 'default' | 'action'
providerId?: string
env: Readonly<Record<string, string>>
prompts: readonly SetupPrompt[]
currentWhen?: SetupCondition
}
interface PromptState {
setup: CapabilitySetupDefinition
optionId: string
currentValues: ReadonlyMap<string, string>
values: Record<string, string>
}
const SKIP_OPTION_ID = '__skip-capability-setup__'
/** Stages a capability transition into a larger setup run without losing prompt context. */
export function stageCapabilitySetupTransition(
currentValues: Map<string, string>,
values: Record<string, string>,
remove: Set<string>,
transition: EnvCapabilitySetupTransition
): void {
for (const key of transition.remove) {
currentValues.delete(key)
Reflect.deleteProperty(values, key)
remove.add(key)
}
for (const [key, value] of Object.entries(transition.values)) {
currentValues.set(key, value)
values[key] = value
remove.delete(key)
}
}
function resolveHint(
hint: SetupHint | undefined,
context: CapabilitySetupContext
): string | undefined {
if (!hint || typeof hint === 'string') return hint
return context.containerized ? hint.containerized : hint.development
}
/** Adds the standard status marker without discarding an option's explanatory hint. */
export function markCurrentlyUsed(hint: string | undefined, current: boolean): string | undefined {
if (!current) return hint
return hint ? `${hint} · Currently used` : 'Currently used'
}
function promptKeys(prompts: readonly SetupPrompt[] = []): string[] {
return prompts.flatMap((prompt) => {
if (prompt.type === 'field' || prompt.type === 'confirm') return [prompt.key]
return prompt.options.flatMap((option) => [
...Object.keys(option.env ?? {}),
...promptKeys(option.prompts),
])
})
}
function providerSetupFields(
setup: CapabilitySetupDefinition,
providerId: string
): readonly string[] {
const provider = setup.definition.providers.find((candidate) => candidate.id === providerId)
if (!provider) throw new Error(`Capability ${setup.definition.id} has no provider ${providerId}`)
const providerSetup = setup.providers[providerId]
if (!providerSetup) throw new Error(`Setup ${setup.definition.id} has no provider ${providerId}`)
return [
...(provider.activation.mode === 'enabled' ? [provider.activation.key] : []),
...Object.keys(providerSetup.env ?? {}),
...promptKeys(providerSetup.prompts),
]
}
/** Returns fields the setup flow writes or prompts for, including inferred selectors and flags. */
export function getCapabilitySetupFields(setup: CapabilitySetupDefinition): readonly string[] {
return [
...new Set([
...(setup.definition.strategy === 'selected' && setup.definition.selectorKey
? [setup.definition.selectorKey]
: []),
...setup.definition.providers.flatMap((provider) =>
provider.activation.mode === 'enabled' ? [provider.activation.key] : []
),
...Object.entries(setup.providers).flatMap(([providerId]) =>
providerSetupFields(setup, providerId)
),
...Object.values(setup.actions).flatMap((action) => Object.keys(action.env ?? {})),
]),
]
}
/** Expands the runtime providers and CLI-only actions into renderable options. */
export function getCapabilitySetupOptions(
setup: CapabilitySetupDefinition
): readonly ResolvedSetupOption[] {
const definition = setup.definition
const options: ResolvedSetupOption[] = definition.providers.map((provider) => {
const providerSetup = setup.providers[provider.id]
if (!providerSetup) throw new Error(`Setup ${definition.id} has no provider ${provider.id}`)
return {
id: provider.id,
label: provider.label,
hint: providerSetup.hint,
kind: 'provider',
providerId: provider.id,
env: providerSetup.env ?? {},
prompts: providerSetup.prompts,
currentWhen: providerSetup.currentWhen,
}
})
if (definition.strategy === 'selected' && definition.defaultProvider.kind === 'built-in') {
options.push({
id: definition.defaultProvider.id,
label: definition.defaultProvider.label,
hint: setup.defaultOption?.hint,
kind: 'default',
env: {},
prompts: [],
})
}
for (const [id, action] of Object.entries(setup.actions)) {
options.push({
id,
label: action.label,
hint: action.hint,
kind: 'action',
env: action.env ?? {},
prompts: [],
currentWhen: action.currentWhen,
})
}
const byId = new Map(options.map((option) => [option.id, option]))
return setup.optionOrder.map((id) => {
const option = byId.get(String(id))
if (!option) throw new Error(`Setup ${definition.id} option order references ${String(id)}`)
return option
})
}
/** Resolves the setup option representing the effective current configuration, if one exists. */
export function resolveCurrentCapabilitySetupOptionId(
setup: CapabilitySetupDefinition,
values: EnvCapabilityValues
): string | undefined {
const options = getCapabilitySetupOptions(setup)
const explicitAction = options.find(
(option) =>
option.kind === 'action' &&
option.currentWhen &&
matchesSetupCondition(option.currentWhen, values)
)
if (explicitAction) return explicitAction.id
const inspection = inspectCapability(setup.definition, values)
const selectedProviderId =
inspection.strategy === 'selected'
? (inspection.providerId ?? inspection.providers.find((provider) => provider.active)?.id)
: (inspection.providerIds[0] ?? inspection.providers.find((provider) => provider.active)?.id)
if (selectedProviderId) {
const provider = options.find(
(option) =>
option.kind === 'provider' &&
option.providerId === selectedProviderId &&
(!option.currentWhen || matchesSetupCondition(option.currentWhen, values))
)
if (provider) return provider.id
}
if (
setup.definition.strategy === 'selected' &&
setup.definition.defaultProvider.kind === 'built-in'
) {
return setup.definition.defaultProvider.id
}
const firstAction = options.find((option) => option.kind === 'action')
if (firstAction) return firstAction.id
if (options.length === 0) {
throw new Error(`Capability ${setup.definition.id} has no setup options`)
}
return undefined
}
/** Applies selector and activation inference to the CLI-entered values. */
export function getCapabilitySetupDraftValues(
setup: CapabilitySetupDefinition,
optionId: string,
promptedValues: Readonly<Record<string, string>>
): Record<string, string> {
const definition = setup.definition
const option = getCapabilitySetupOptions(setup).find((candidate) => candidate.id === optionId)
if (!option) throw new Error(`Capability ${definition.id} has no setup option ${optionId}`)
const values: Record<string, string> = { ...option.env }
if (definition.strategy === 'selected' && definition.selectorKey) {
if (option.providerId) values[definition.selectorKey] = option.providerId
else if (option.kind === 'default' && option.id === definition.defaultProvider.id) {
values[definition.selectorKey] = option.id
}
}
for (const provider of definition.providers) {
if (provider.activation.mode !== 'enabled') continue
values[provider.activation.key] = provider.id === option.providerId ? 'true' : 'false'
}
Object.assign(values, promptedValues)
return values
}
function copyValues(values: EnvCapabilityValues): Record<string, EnvCapabilityValue> {
return values instanceof Map
? Object.fromEntries(values)
: { ...(values as Readonly<Record<string, EnvCapabilityValue>>) }
}
function fieldsToReplace(
setup: CapabilitySetupDefinition,
option: ResolvedSetupOption
): readonly string[] {
if (setup.definition.strategy === 'fallback' && option.providerId) {
return providerSetupFields(setup, option.providerId)
}
const selectedProviderFields = new Set(
setup.definition.providers
.filter((provider) => provider.id === option.providerId)
.flatMap(getProviderFields)
)
const inactiveProviderFields = setup.definition.providers
.filter((provider) => provider.id !== option.providerId)
.flatMap(getProviderFields)
.filter((field) => !selectedProviderFields.has(field))
return [...new Set([...getCapabilitySetupFields(setup), ...inactiveProviderFields])]
}
function proposedCapabilitySetupValues(
setup: CapabilitySetupDefinition,
option: ResolvedSetupOption,
values: Readonly<Record<string, string>>,
currentValues: EnvCapabilityValues
): Record<string, EnvCapabilityValue> {
const proposed = copyValues(currentValues)
for (const key of fieldsToReplace(setup, option)) Reflect.deleteProperty(proposed, key)
Object.assign(proposed, values)
return proposed
}
/** Returns provider requirements discovered after earlier prompts have been answered. */
export function getCapabilitySetupProviderMissingFields(
setup: CapabilitySetupDefinition,
optionId: string,
promptedValues: Readonly<Record<string, string>>,
currentValues: EnvCapabilityValues
): readonly string[] {
const option = getCapabilitySetupOptions(setup).find((candidate) => candidate.id === optionId)
if (!option?.providerId) return []
const values = getCapabilitySetupDraftValues(setup, optionId, promptedValues)
const inspection = inspectCapability(
setup.definition,
proposedCapabilitySetupValues(setup, option, values, currentValues)
)
return (
inspection.providers.find((provider) => provider.id === option.providerId)?.missingFields ?? []
)
}
/** Builds and validates the complete environment transition for one setup option. */
export function buildCapabilitySetupTransition(
setup: CapabilitySetupDefinition,
optionId: string,
promptedValues: Readonly<Record<string, string>>,
currentValues: EnvCapabilityValues
): EnvCapabilitySetupTransition {
const definition = setup.definition
const option = getCapabilitySetupOptions(setup).find((candidate) => candidate.id === optionId)
if (!option) throw new Error(`Capability ${definition.id} has no setup option ${optionId}`)
const values = getCapabilitySetupDraftValues(setup, optionId, promptedValues)
const ownedFields = getCapabilitySetupFields(setup)
const unexpected = Object.keys(values).filter((key) => !ownedFields.includes(key))
if (unexpected.length > 0) {
throw new Error(
`Capability ${definition.id} setup produced unowned fields: ${unexpected.join(', ')}`
)
}
const replacedFields = fieldsToReplace(setup, option)
const remove = replacedFields.filter((key) => !Object.hasOwn(values, key))
const proposed = proposedCapabilitySetupValues(setup, option, values, currentValues)
const inspection = inspectCapability(definition, proposed)
if (inspection.error) throw inspection.error
if (option.providerId) {
const provider = inspection.providers.find((candidate) => candidate.id === option.providerId)
if (!provider || provider.state !== 'ready') {
throw new EnvCapabilityConfigurationError(
definition.id,
`${definition.label} setup option ${option.id} did not configure ${option.providerId}`
)
}
if (inspection.strategy === 'selected' && inspection.providerId !== option.providerId) {
throw new EnvCapabilityConfigurationError(
definition.id,
`${definition.label} setup selected ${inspection.providerId ?? 'nothing'} instead of ${option.providerId}`
)
}
} else {
const activeProvider = inspection.providers.find((provider) => provider.active)
if (activeProvider) {
throw new EnvCapabilityConfigurationError(
definition.id,
`${definition.label} setup option ${option.id} left ${activeProvider.id} active`
)
}
}
return { values, remove }
}
function proposedPromptValues(state: PromptState): EnvCapabilityValues {
const option = getCapabilitySetupOptions(state.setup).find(
(candidate) => candidate.id === state.optionId
)
if (!option) {
throw new Error(`Capability ${state.setup.definition.id} has no setup option ${state.optionId}`)
}
return proposedCapabilitySetupValues(
state.setup,
option,
getCapabilitySetupDraftValues(state.setup, state.optionId, state.values),
state.currentValues
)
}
function shouldRenderPrompt(prompt: SetupPrompt, state: PromptState): boolean {
if (!prompt.when) return true
if (prompt.when.kind === 'provider-missing-field') {
return getCapabilitySetupProviderMissingFields(
state.setup,
state.optionId,
state.values,
state.currentValues
).includes(prompt.when.key)
}
return matchesSetupCondition(prompt.when, proposedPromptValues(state))
}
/** Formats current-value status without ever rendering the configured value. */
export function formatCapabilitySetupFieldMessage(
prompt: Extract<SetupPrompt, { type: 'field' }>,
current: boolean,
keepExisting = false
): string {
const label = prompt.message ?? prompt.key
const status = current ? ' (Currently used)' : ''
const hint = prompt.hint ? ` — ${prompt.hint}` : ''
const preservation = current && keepExisting ? '; leave empty to keep it' : ''
return `${label}${status}${hint}${preservation}`
}
async function promptField(
prompt: Extract<SetupPrompt, { type: 'field' }>,
state: PromptState
): Promise<void> {
const existing = state.currentValues.get(prompt.key)
const current = hasEnvCapabilityValue(state.currentValues, prompt.key)
const draft = getCapabilitySetupDraftValues(state.setup, state.optionId, state.values)
const initialValue = existing ?? draft[prompt.key] ?? prompt.defaultValue
const validate = (value: string): string | undefined => {
if (!value) return prompt.required && !existing ? 'required' : undefined
return prompt.validate
? validateCapabilityFieldInput(state.setup.definition, prompt.key, value)
: undefined
}
let value: string
if (prompt.input === 'secret') {
value = await p.password({
message: formatCapabilitySetupFieldMessage(prompt, current, true),
validate,
})
if (!value && existing) value = existing
} else {
value = await p.text({
message: formatCapabilitySetupFieldMessage(prompt, current),
initialValue,
defaultValue: initialValue ? undefined : prompt.defaultValue,
validate,
})
}
if (value) state.values[prompt.key] = value
else Reflect.deleteProperty(state.values, prompt.key)
}
async function renderPrompts(prompts: readonly SetupPrompt[], state: PromptState): Promise<void> {
for (const prompt of prompts) {
if (!shouldRenderPrompt(prompt, state)) continue
if (prompt.type === 'field') {
await promptField(prompt, state)
continue
}
if (prompt.type === 'confirm') {
const proposed = proposedPromptValues(state)
const current = hasEnvCapabilityValue(state.currentValues, prompt.key)
const enabled = await p.confirm({
message: `${prompt.message}${current ? ' (Currently used)' : ''}`,
initialValue: hasEnvCapabilityValue(proposed, prompt.key)
? isTruthyEnvCapabilityValue(proposed, prompt.key)
: (prompt.defaultValue ?? false),
})
state.values[prompt.key] = enabled ? 'true' : 'false'
continue
}
const currentOption = prompt.options.find(
(option) =>
option.currentWhen && matchesSetupCondition(option.currentWhen, state.currentValues)
)
const initialOption = currentOption ?? prompt.options[0]
if (!initialOption) throw new Error(`Setup choice ${prompt.id} has no options`)
const selectedId = await p.select({
message: prompt.message,
options: prompt.options.map((option) => ({
value: option.id,
label: option.label,
hint: markCurrentlyUsed(option.hint, option.id === currentOption?.id),
})),
initialValue: initialOption.id,
})
const selected = prompt.options.find((option) => option.id === selectedId)
if (!selected) {
throw new Error(`Setup choice ${prompt.id} returned unknown option ${selectedId}`)
}
Object.assign(state.values, selected.env ?? {})
await renderPrompts(selected.prompts ?? [], state)
}
}
async function promptSelectedCapabilitySetup(
setup: CapabilitySetupDefinition,
selected: ResolvedSetupOption,
currentValues: ReadonlyMap<string, string>
): Promise<EnvCapabilitySetupTransition> {
const state: PromptState = {
setup,
optionId: selected.id,
currentValues,
values: {},
}
await renderPrompts(selected.prompts, state)
return buildCapabilitySetupTransition(setup, selected.id, state.values, currentValues)
}
/** Renders a CLI-owned capability setup and returns its validated environment transition. */
export async function promptCapabilitySetup(
setup: CapabilitySetupDefinition,
currentValues: ReadonlyMap<string, string>,
context: CapabilitySetupContext
): Promise<EnvCapabilitySetupTransition> {
const options = getCapabilitySetupOptions(setup)
const currentOptionId = resolveCurrentCapabilitySetupOptionId(setup, currentValues)
const selectedOptionId = await p.select({
message: setup.message,
options: options.map((option) => ({
value: option.id,
label: option.label,
hint: markCurrentlyUsed(resolveHint(option.hint, context), option.id === currentOptionId),
})),
initialValue: currentOptionId,
})
const selected = options.find((option) => option.id === selectedOptionId)
if (!selected) {
throw new Error(
`Capability ${setup.definition.id} returned unknown setup option ${selectedOptionId}`
)
}
return promptSelectedCapabilitySetup(setup, selected, currentValues)
}
/** Offers capability providers while allowing the user to leave configuration unchanged. */
export async function promptOptionalCapabilitySetup(
setup: CapabilitySetupDefinition,
currentValues: ReadonlyMap<string, string>,
context: CapabilitySetupContext,
skipHint: string
): Promise<EnvCapabilitySetupTransition | null> {
const options = getCapabilitySetupOptions(setup)
if (options.some((option) => option.id === SKIP_OPTION_ID)) {
throw new Error(`Capability ${setup.definition.id} uses reserved option ${SKIP_OPTION_ID}`)
}
const currentOptionId = resolveCurrentCapabilitySetupOptionId(setup, currentValues)
const selectedOptionId = await p.select({
message: setup.message,
options: [
...options.map((option) => ({
value: option.id,
label: option.label,
hint: markCurrentlyUsed(resolveHint(option.hint, context), option.id === currentOptionId),
})),
{
value: SKIP_OPTION_ID,
label: 'Not now',
hint: currentOptionId ? 'leave the current configuration unchanged' : skipHint,
},
],
initialValue: currentOptionId ?? options[0]?.id,
})
if (selectedOptionId === SKIP_OPTION_ID) return null
const selected = options.find((option) => option.id === selectedOptionId)
if (!selected) {
throw new Error(
`Capability ${setup.definition.id} returned unknown setup option ${selectedOptionId}`
)
}
return promptSelectedCapabilitySetup(setup, selected, currentValues)
}