-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathsteps.ts
More file actions
422 lines (402 loc) · 15.7 KB
/
Copy pathsteps.ts
File metadata and controls
422 lines (402 loc) · 15.7 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
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, hasMailProvider, 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
}
type StorageBackend = 'local' | 's3' | 's3compat' | 'azure' | 'gcs'
function detectStorageBackend(vars: Map<string, string>): StorageBackend {
if (vars.get('AZURE_CONNECTION_STRING') || vars.get('AZURE_ACCOUNT_NAME')) return 'azure'
if (vars.get('S3_ENDPOINT')) return 's3compat'
if (vars.get('S3_BUCKET_NAME') || vars.get('AWS_REGION')) return 's3'
if (vars.get('GCS_BUCKET_NAME')) return 'gcs'
return 'local'
}
async function required(message: string, initialValue?: string): Promise<string> {
return p.text({ message, initialValue, validate: (v) => (v ? undefined : 'required') })
}
/**
* Custom-flow storage step. Local disk is the default; a cloud backend is
* strongly recommended for containerized deployments (uploads are ephemeral
* there). Returns the env vars for the chosen backend, or null to keep local.
*/
export async function promptStorage(
vars: Map<string, string>,
containerized: boolean
): Promise<Record<string, string> | null> {
const current = detectStorageBackend(vars)
const backend = await p.select<StorageBackend>({
message: 'File storage?',
options: [
{
value: 'local',
label: 'Local disk',
hint: containerized
? 'files live in the container — LOST on restart; fine only for evaluation'
: 'fine for local dev (external-fetch flows like Instagram publish need cloud storage)',
},
{ value: 's3', label: 'AWS S3', hint: 'region + bucket; keys optional with IAM/IRSA' },
{
value: 's3compat',
label: 'S3-compatible (R2, MinIO, B2)',
hint: 'custom endpoint — fully self-hostable with MinIO',
},
{ value: 'azure', label: 'Azure Blob', hint: 'connection string or account name + key' },
{
value: 'gcs',
label: 'Google Cloud Storage',
hint: 'bucket; credentials via ADC by default',
},
],
initialValue: current,
})
if (backend === 'local') return null
const values: Record<string, string> = {}
if (backend === 's3' || backend === 's3compat') {
if (backend === 's3compat') {
values.S3_ENDPOINT = await required(
'S3_ENDPOINT (e.g. https://<account>.r2.cloudflarestorage.com)',
vars.get('S3_ENDPOINT')
)
const pathStyle = await p.confirm({
message: 'Force path-style addressing? (required for MinIO/Ceph, not for R2)',
initialValue: false,
})
if (pathStyle) values.S3_FORCE_PATH_STYLE = 'true'
}
values.AWS_REGION = await required(
'AWS_REGION',
vars.get('AWS_REGION') ?? (backend === 's3compat' ? 'auto' : undefined)
)
values.S3_BUCKET_NAME = await required('S3_BUCKET_NAME', vars.get('S3_BUCKET_NAME'))
const accessKey = await p.password({
message: 'AWS_ACCESS_KEY_ID (empty = IAM/instance credential chain)',
})
if (accessKey) {
values.AWS_ACCESS_KEY_ID = accessKey
values.AWS_SECRET_ACCESS_KEY = await p.password({
message: 'AWS_SECRET_ACCESS_KEY',
validate: (v) => (v ? undefined : 'required when an access key id is set'),
})
}
} else if (backend === 'azure') {
const connectionString = await p.password({
message: 'AZURE_CONNECTION_STRING (empty = use account name + key)',
})
if (connectionString) {
values.AZURE_CONNECTION_STRING = connectionString
} else {
values.AZURE_ACCOUNT_NAME = await required(
'AZURE_ACCOUNT_NAME',
vars.get('AZURE_ACCOUNT_NAME')
)
values.AZURE_ACCOUNT_KEY = await p.password({
message: 'AZURE_ACCOUNT_KEY',
validate: (v) => (v ? undefined : 'required'),
})
}
values.AZURE_STORAGE_CONTAINER_NAME = await required(
'AZURE_STORAGE_CONTAINER_NAME',
vars.get('AZURE_STORAGE_CONTAINER_NAME') ?? 'sim-files'
)
} else {
values.GCS_BUCKET_NAME = await required('GCS_BUCKET_NAME', vars.get('GCS_BUCKET_NAME'))
p.log.info(
theme.muted(
'Credentials use Application Default Credentials unless GCS_CREDENTIALS_JSON is set.'
)
)
}
return values
}
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) ? 'already configured' : 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,
initialValue: vars.get(provider.idKey),
validate: (v) => (v ? undefined : 'required'),
})
values[provider.secretKey] = await p.password({
message: provider.secretKey,
validate: (v) => (v ? undefined : 'required'),
})
}
return values
}
/** Email step: console logging is the default; MailHog is the one-tap local option. */
export async function promptEmail(vars: Map<string, string>): Promise<Record<string, string>> {
const choice = await p.select({
message: 'Email sending?',
options: [
{
value: 'console',
label: 'None',
hint: 'emails are logged to the console — fine for local',
},
{ value: 'mailhog', label: 'MailHog (local)', hint: 'wires SMTP to localhost:1025' },
{ value: 'resend', label: 'Resend', hint: 'paste an API key' },
{ value: 'smtp', label: 'SMTP', hint: 'any SMTP relay' },
],
initialValue: hasMailProvider(vars) ? (vars.get('SMTP_HOST') ? 'smtp' : 'resend') : 'console',
})
if (choice === 'console') return {}
if (choice === 'mailhog') return { SMTP_HOST: 'localhost', SMTP_PORT: '1025' }
if (choice === 'resend') {
return {
RESEND_API_KEY: await p.password({
message: 'RESEND_API_KEY',
validate: (v) => (v ? undefined : 'required'),
}),
}
}
const values: Record<string, string> = {
SMTP_HOST: await p.text({
message: 'SMTP_HOST',
initialValue: vars.get('SMTP_HOST'),
validate: (v) => (v ? undefined : 'required'),
}),
SMTP_PORT: await p.text({ message: 'SMTP_PORT', initialValue: vars.get('SMTP_PORT') ?? '587' }),
}
const user = await p.text({
message: 'SMTP_USER (empty for unauthenticated relays)',
defaultValue: '',
})
if (user) {
values.SMTP_USER = user
values.SMTP_PASS = await p.password({ message: 'SMTP_PASS' })
}
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
}