-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathsteps.ts
More file actions
292 lines (277 loc) · 11.2 KB
/
Copy pathsteps.ts
File metadata and controls
292 lines (277 loc) · 11.2 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
import { KNOWLEDGE_EMBEDDINGS_SETUP } from './capability-config.ts'
import {
type CapabilitySetupContext,
type EnvCapabilitySetupTransition,
promptOptionalCapabilitySetup,
} from './capability-setup.ts'
import { browserKeyFlow } from './cli-auth.ts'
import type { Detection } from './detect.ts'
import {
type EnvFile,
generateSecret,
isPlaceholder,
isTruthy,
isUsableSecret,
SECRET_KEYS,
secretRequirement,
} from './env-files.ts'
import * as p from './prompter.ts'
import { link, theme } from './theme.ts'
import { FLAG_TWINS, LOGIN_PROVIDERS, SELF_HOST_UNLOCKS } from './twins.ts'
/** Where the Chat key is minted when SIM_CLI_AUTH_ORIGIN is unset. */
const DEFAULT_CLI_AUTH_ORIGIN = 'https://www.sim.ai'
/** Reuses existing valid secrets (never regenerates them) and generates the rest. */
export function collectSecrets(existing: EnvFile): Record<string, string> {
const secrets: Record<string, string> = {}
const generated: string[] = []
const replaced: string[] = []
for (const key of SECRET_KEYS) {
const current = existing.vars.get(key)
if (current && isUsableSecret(key, current)) {
secrets[key] = current
} else {
secrets[key] = generateSecret()
// A key the app would reject never successfully encrypted anything, so
// replacing it cannot orphan existing ciphertext.
if (current && !isPlaceholder(current)) replaced.push(key)
else generated.push(key)
}
}
if (replaced.length > 0) {
const detail = replaced.map((key) => `${key} (${secretRequirement(key)})`).join(', ')
p.log.warn(`Replaced ${detail} — the app rejects the existing value at runtime.`)
}
if (generated.length > 0) {
p.log.step(`Generated ${generated.join(', ')}`)
}
return secrets
}
export async function promptCopilotKey(existing?: string): Promise<string | null> {
if (existing) {
const keep = await p.confirm({
message: 'COPILOT_API_KEY is already set — keep it?',
initialValue: true,
})
if (keep) return existing
}
p.log.info('Chat is how you talk to Sim — build and manage everything in natural language.')
const wants = await p.confirm({
message: 'Generate your Chat API key in the browser?',
initialValue: true,
})
if (!wants) {
p.log.info(theme.muted('Skipping — the Chat module stays hidden until you re-run setup.'))
return null
}
const key = await browserKeyFlow(process.env.SIM_CLI_AUTH_ORIGIN ?? DEFAULT_CLI_AUTH_ORIGIN)
if (!key) {
// Both halves, because the caller writes the opt-out for a null key: a
// hand-set credential alone restores capability while Chat stays hidden.
p.log.warn(
'No key received — re-run bun run setup to retry, or set COPILOT_API_KEY and NEXT_PUBLIC_CHAT_DISABLED=false yourself.'
)
return null
}
return key
}
/**
* Hides the Chat module when the user skipped the chat key, so a fresh install
* gets no Chat surfaces rather than ones that reject every message. Written in
* both directions on every run, so obtaining a key later un-hides it.
*
* Only the wizard writes this. Chat is on by default everywhere else, which is
* what keeps existing deployments unaffected.
*/
export function chatFlagValues(copilotKey: string | null): Record<string, string> {
return { NEXT_PUBLIC_CHAT_DISABLED: copilotKey ? 'false' : 'true' }
}
/**
* Escape hatch for Sim devs pointing an install at a non-prod mothership:
*
* SIM_CLI_AUTH_ORIGIN=https://www.staging.sim.ai \
* SIM_AGENT_API_URL=https://www.staging.copilot.sim.ai \
* bun run setup
*
* The two belong together — SIM_CLI_AUTH_ORIGIN decides where the Chat key is
* minted, SIM_AGENT_API_URL decides which backend validates it, and a key from
* one environment is rejected by the other. Persisting the URL keeps later
* `docker compose up` / dev runs on the same backend instead of silently
* reverting to prod once the shell that exported it is gone.
*/
export function mothershipOverride(): Record<string, string> {
const agentUrl = process.env.SIM_AGENT_API_URL
const authOrigin = process.env.SIM_CLI_AUTH_ORIGIN
// Either half alone produces the same cross-environment rejection, just in
// opposite directions — mint here, validate there. Warning on only one of them
// would leave the other silent while the copy claims both matter.
if (authOrigin && !agentUrl) {
p.log.warn(
`SIM_CLI_AUTH_ORIGIN mints the Chat key at ${authOrigin}, but SIM_AGENT_API_URL is unset — the app validates against production, which will reject that key. Set both, or neither.`
)
} else if (agentUrl && !authOrigin) {
p.log.warn(
`SIM_AGENT_API_URL points the app at ${agentUrl}, but SIM_CLI_AUTH_ORIGIN is unset — the Chat key is minted at ${DEFAULT_CLI_AUTH_ORIGIN}, which that backend will reject. Set both, or neither.`
)
}
if (!agentUrl) return {}
p.log.step(`Using mothership ${agentUrl} (SIM_AGENT_API_URL)`)
return { SIM_AGENT_API_URL: agentUrl }
}
export async function promptLlmKeys(
detection: Detection,
custom: boolean
): Promise<Record<string, string>> {
const values: Record<string, string> = {}
if (detection.shellLlmKeys.length > 0) {
const adopt = await p.multiselect({
message: 'Found LLM API keys in your shell — copy into apps/sim/.env?',
options: detection.shellLlmKeys.map((key) => ({ value: key, label: key })),
initialValues: detection.shellLlmKeys,
})
for (const key of adopt) {
const value = process.env[key]
if (!value) throw new Error(`${key} disappeared from the environment mid-run`)
values[key] = value
}
}
if (detection.ollamaReachable) {
const useOllama = await p.confirm({
message: 'Ollama is running on :11434 — wire it up for local models?',
initialValue: true,
})
if (useOllama) values.OLLAMA_URL = 'http://localhost:11434'
}
if (custom && Object.keys(values).length === 0 && detection.shellLlmKeys.length === 0) {
p.log.info(
theme.muted('No LLM keys configured — you can add keys per-workspace in the UI later (BYOK).')
)
}
return values
}
/** Configures knowledge embeddings while allowing the user to explicitly defer them. */
export function promptKnowledgeEmbeddings(
currentValues: ReadonlyMap<string, string>,
context: CapabilitySetupContext
): Promise<EnvCapabilitySetupTransition | null> {
return promptOptionalCapabilitySetup(
KNOWLEDGE_EMBEDDINGS_SETUP,
currentValues,
context,
'knowledge-base indexing and semantic search will remain unavailable'
)
}
const PROVIDER_CONSOLES: Record<string, string> = {
google: 'https://console.cloud.google.com/apis/credentials',
github: 'https://github.com/settings/developers',
microsoft: 'https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade',
}
/** Sign-in providers step: credentials in, exact redirect URIs out. */
export async function promptSignInProviders(
vars: Map<string, string>,
appUrl: string
): Promise<Record<string, string>> {
const configured = LOGIN_PROVIDERS.filter((prov) => vars.get(prov.idKey)).map((prov) => prov.id)
const wanted = await p.multiselect({
message: 'Social sign-in providers? (email/password login works without any)',
options: LOGIN_PROVIDERS.map((prov) => ({
value: prov.id,
label: prov.label,
hint: configured.includes(prov.id) ? 'Currently used' : undefined,
})),
initialValues: configured,
})
const values: Record<string, string> = {}
for (const id of wanted) {
const provider = LOGIN_PROVIDERS.find((prov) => prov.id === id)
if (!provider) throw new Error(`unknown provider ${id}`)
p.log.info(
`${provider.label}: create an OAuth app at ${link(PROVIDER_CONSOLES[id], PROVIDER_CONSOLES[id])}\n Redirect URI: ${theme.command(`${appUrl}/api/auth/callback/${id}`)}`
)
values[provider.idKey] = await p.text({
message: `${provider.idKey}${vars.has(provider.idKey) ? ' (Currently used)' : ''}`,
initialValue: vars.get(provider.idKey),
validate: (v) => (v ? undefined : 'required'),
})
const existingSecret = vars.get(provider.secretKey)
const secret = await p.password({
message: existingSecret
? `${provider.secretKey} (Currently used); leave empty to keep it`
: provider.secretKey,
validate: (value) => (value || existingSecret ? undefined : 'required'),
})
const resolvedSecret = secret || existingSecret
if (!resolvedSecret) throw new Error(`${provider.secretKey} was not provided`)
values[provider.secretKey] = resolvedSecret
}
return values
}
export interface SecurityStepResult {
sim: Record<string, string>
mirrorToRealtime: Record<string, string>
}
/** Auth loosening + admin key. DISABLE_AUTH must reach BOTH env files. */
export async function promptSecurity(vars: Map<string, string>): Promise<SecurityStepResult> {
const sim: Record<string, string> = {}
const mirrorToRealtime: Record<string, string> = {}
const disableAuth = await p.confirm({
message: 'Disable auth entirely? (anonymous access — ONLY for a private network)',
initialValue: isTruthy(vars.get('DISABLE_AUTH')),
})
if (disableAuth) {
p.log.warn('Anyone who can reach this instance has full access. Never expose it publicly.')
sim.DISABLE_AUTH = 'true'
mirrorToRealtime.DISABLE_AUTH = 'true'
}
const privateHosts = await p.confirm({
message:
'Allow DB/connector tools to reach private hosts? (Docker/K8s service names, localhost — loosens the SSRF guard)',
initialValue: isTruthy(vars.get('ALLOW_PRIVATE_DATABASE_HOSTS')),
})
if (privateHosts) sim.ALLOW_PRIVATE_DATABASE_HOSTS = 'true'
const existingAdminKey = vars.get('ADMIN_API_KEY')
if (!existingAdminKey || isPlaceholder(existingAdminKey)) {
const wantsAdmin = await p.confirm({
message: 'Generate an ADMIN_API_KEY? (enables the admin API for workflow export/import)',
initialValue: false,
})
if (wantsAdmin) {
sim.ADMIN_API_KEY = generateSecret()
p.log.step('Generated ADMIN_API_KEY')
}
}
return { sim, mirrorToRealtime }
}
/** Self-host feature unlocks — always writes BOTH members of each twin pair. */
export async function promptUnlocks(vars: Map<string, string>): Promise<Record<string, string>> {
const selected = await p.multiselect({
message: 'Unlock self-host features? (bypasses hosted plan gating)',
options: SELF_HOST_UNLOCKS.map((unlock) => ({
value: unlock.server,
label: unlock.label,
hint: unlock.hint || undefined,
})),
initialValues: SELF_HOST_UNLOCKS.filter((u) => isTruthy(vars.get(u.server))).map(
(u) => u.server
),
})
if (selected.length === 0) return {}
const flags = new Set(selected)
if (flags.has('ENTERPRISE_ENABLED')) {
p.log.info(
theme.muted(
'The enterprise switch covers every feature below — pick individual ones only to override it.'
)
)
}
if (flags.has('ACCESS_CONTROL_ENABLED') && !flags.has('ORGANIZATIONS_ENABLED')) {
flags.add('ORGANIZATIONS_ENABLED')
p.log.info(theme.muted('Access control requires organizations — enabling both.'))
}
const values: Record<string, string> = {}
for (const server of flags) {
values[server] = 'true'
const twin = FLAG_TWINS.find((pair) => pair.server === server)
if (twin) values[twin.client] = 'true'
}
return values
}