@open-mercato/starter 0.10.1-develop.7983.1.fd91207efe → 0.10.1-develop.7994.1.c9f31972aa

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@
7
7
  import { spawnSync } from 'node:child_process'
8
8
  import fs from 'node:fs'
9
9
  import net from 'node:net'
10
+ import readline from 'node:readline'
10
11
 
11
12
  export const INFRA_PROBE_TIMEOUT_MS = 20_000
12
13
  export const NO_INFRA_FLAG = '--no-infra'
@@ -126,6 +127,157 @@ export async function findPortConflicts(services, { running = new Set(), probe =
126
127
  return conflicts
127
128
  }
128
129
 
130
+ function diagnosticText(value) {
131
+ return String(value ?? '').replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, '').replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ')
132
+ }
133
+
134
+ function diagnosticColors({ output = process.stdout, env = process.env } = {}) {
135
+ const enabled = Boolean(output?.isTTY) && !Object.hasOwn(env, 'NO_COLOR') && env.TERM !== 'dumb'
136
+ const paint = (code, value) => enabled ? `\u001b[${code}m${diagnosticText(value)}\u001b[0m` : diagnosticText(value)
137
+ return {
138
+ service: (value) => paint(36, value),
139
+ port: (value) => paint(33, value),
140
+ variable: (value) => paint(35, value),
141
+ command: (value) => paint(32, value),
142
+ pid: (value) => paint(33, value),
143
+ }
144
+ }
145
+
146
+ // For each conflicting service, the next free port above the taken one, so the
147
+ // message can name the exact .env line instead of "pick a free port".
148
+ export async function suggestFreePorts(conflicts, { probe = probeTcpPort, span = 100, reserved = [] } = {}) {
149
+ const claimed = new Set([...reserved, ...conflicts.map((entry) => entry.port)])
150
+ const suggested = []
151
+ for (const entry of conflicts) {
152
+ let freePort = null
153
+ for (let candidate = entry.port + 1; candidate <= Math.min(entry.port + span, 65535); candidate += 1) {
154
+ if (claimed.has(candidate)) continue
155
+ if (!(await probe(candidate))) {
156
+ freePort = candidate
157
+ claimed.add(candidate)
158
+ break
159
+ }
160
+ }
161
+ suggested.push(freePort ? { ...entry, suggestedPort: freePort } : entry)
162
+ }
163
+ return suggested
164
+ }
165
+
166
+ export function parseListenerProcesses(raw, { format = 'lsof' } = {}) {
167
+ const found = new Map()
168
+ const add = (pid, command) => {
169
+ const number = Number(pid)
170
+ if (Number.isSafeInteger(number) && number > 0) found.set(number, { pid: number, command: diagnosticText(command).trim() })
171
+ }
172
+ if (format === 'powershell') {
173
+ try {
174
+ const parsed = JSON.parse(String(raw ?? '').trim() || '[]')
175
+ for (const entry of Array.isArray(parsed) ? parsed : [parsed]) add(entry.pid, entry.command)
176
+ } catch {}
177
+ } else if (format === 'ss') {
178
+ for (const match of String(raw ?? '').matchAll(/\("([^"\r\n]*)",pid=(\d+),/g)) add(match[2], match[1])
179
+ } else {
180
+ let pid
181
+ for (const line of String(raw ?? '').split(/\r?\n/)) {
182
+ if (/^p\d+$/.test(line)) { pid = Number(line.slice(1)); add(pid, '') }
183
+ else if (line.startsWith('c') && pid) add(pid, line.slice(1))
184
+ }
185
+ }
186
+ return [...found.values()]
187
+ }
188
+
189
+ export function findPortProcessOwners(port, { platform = process.platform, spawnSyncImpl = spawnSync, timeoutMs = 2000 } = {}) {
190
+ if (!Number.isInteger(port) || port < 1 || port > 65535) return []
191
+ const probe = (command, args) => {
192
+ try {
193
+ const result = spawnSyncImpl(command, args, { encoding: 'utf8', timeout: timeoutMs, maxBuffer: 64 * 1024, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
194
+ return !result?.error && result?.status === 0 ? result.stdout : ''
195
+ } catch { return '' }
196
+ }
197
+ if (platform === 'win32') {
198
+ const script = `$owners = Get-NetTCPConnection -State Listen -LocalPort ${port} -ErrorAction Stop | Select-Object -ExpandProperty OwningProcess -Unique; $owners | ForEach-Object { $ownerPid = $_; $ownerProcess = Get-Process -Id $ownerPid -ErrorAction SilentlyContinue; [pscustomobject]@{pid=$ownerPid; command=$ownerProcess.ProcessName} } | ConvertTo-Json -Compress`
199
+ return parseListenerProcesses(probe('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script]), { format: 'powershell' })
200
+ }
201
+ const listeners = parseListenerProcesses(probe('lsof', ['-nP', `-iTCP:${port}`, '-sTCP:LISTEN', '-Fpc']))
202
+ if (listeners.length > 0 || platform !== 'linux') return listeners
203
+ return parseListenerProcesses(probe('ss', ['-ltnp', `sport = :${port}`]), { format: 'ss' })
204
+ }
205
+
206
+ function listenerSummary(entry, colors) {
207
+ if (!entry.processes?.length) return entry.containerName ? `Container: ${diagnosticText(entry.containerName)}` : 'Listener PID: unavailable'
208
+ return `Listener ${entry.processes.length === 1 ? 'PID' : 'PIDs'}: ${entry.processes.map(({ pid, command }) => `${colors.pid(pid)}${command ? ` (${diagnosticText(command)})` : ''}`).join(', ')}`
209
+ }
210
+
211
+ export function findContainerPortOwners(services, { spawnSyncImpl = spawnSync } = {}) {
212
+ const listed = runDockerProbe(['ps', '--all', '--filter', 'status=running', '--filter', 'status=restarting', '--format', '{{.ID}}'], { spawnSyncImpl })
213
+ if (listed?.error || listed?.status !== 0) return []
214
+ const ids = String(listed.stdout ?? '').split(/\s+/).filter((id) => /^[a-f0-9]{12,64}$/.test(id))
215
+ if (ids.length === 0) return []
216
+ const format = '{"name":{{json .Name}},"status":{{json .State.Status}},"health":{{if .State.Health}}{{json .State.Health.Status}}{{else}}""{{end}},"ports":{{json .HostConfig.PortBindings}}}'
217
+ const inspected = runDockerProbe(['inspect', '--format', format, ...ids], { spawnSyncImpl })
218
+ if (inspected?.error || inspected?.status !== 0) return []
219
+ const owners = []
220
+ for (const container of parseComposePsOutput(inspected.stdout)) {
221
+ if (!['running', 'restarting'].includes(container?.status) || !container.ports || typeof container.ports !== 'object') continue
222
+ const hostPorts = new Set(Object.entries(container.ports).flatMap(([containerPort, bindings]) =>
223
+ containerPort.endsWith('/tcp') && Array.isArray(bindings)
224
+ ? bindings.filter((binding) => ['', '0.0.0.0', '127.0.0.1', '::', '::1'].includes(binding.HostIp ?? '')).map((binding) => Number(binding.HostPort))
225
+ : []))
226
+ for (const service of services) {
227
+ if (hostPorts.has(service.port)) owners.push({ ...service, containerName: String(container.name ?? '').replace(/^\//, ''), state: container.status, health: container.health ?? '' })
228
+ }
229
+ }
230
+ return owners
231
+ }
232
+
233
+ export async function requestExistingServicesConsent(problem, {
234
+ nonInteractive = false,
235
+ input = process.stdin,
236
+ output = process.stdout,
237
+ log = console.log,
238
+ question,
239
+ env = process.env,
240
+ } = {}) {
241
+ const colors = diagnosticColors({ output, env })
242
+ const configuredServices = problem.configuredServices === true
243
+ for (const entry of problem.ports ?? problem.services ?? []) {
244
+ const identity = colors.service(entry.containerName || entry.service)
245
+ const state = diagnosticText(entry.state || 'unknown')
246
+ log(` ⚠️ ${identity}${entry.port ? ` on port ${colors.port(entry.port)}` : ''}: ${state}${entry.health ? ` (${diagnosticText(entry.health)})` : ''}`)
247
+ log(` ${listenerSummary(entry, colors)}`)
248
+ if (entry.containerName && (state === 'restarting' || entry.health === 'unhealthy')) log(` Inspect the restart/health failure: ${colors.command(`docker logs ${diagnosticText(entry.containerName)}`)}`)
249
+ }
250
+ log('')
251
+ if (configuredServices) {
252
+ log(' The reachable services are configured in .env and may contain data from another environment.')
253
+ } else {
254
+ log(` Otherwise run ${colors.command('`yarn om stop`')} in the checkout that started the infrastructure, then retry setup.`)
255
+ }
256
+ if (nonInteractive || (!question && !input.isTTY)) {
257
+ log(` Existing services need interactive confirmation. Use ${colors.command('--no-infra')} explicitly to use the services configured in .env.`)
258
+ return false
259
+ }
260
+ log('')
261
+ log(configuredServices ? 'Setup will use these configured services without Docker.' : 'These ports may belong to an earlier Open Mercato run.')
262
+ const prompt = 'Continue using these pre-existing services? [y/N] '
263
+ const reader = question ? null : readline.createInterface({ input, output })
264
+ try {
265
+ const answer = await (question ? question(prompt) : new Promise((resolve) => reader.question(prompt, resolve)))
266
+ if (!/^(y|yes)$/i.test(String(answer).trim())) return false
267
+ log('')
268
+ log(' ⚠️ Continuing with the services configured in .env.')
269
+ if (configuredServices) {
270
+ log(' Configured services will not be started, stopped or reconfigured.')
271
+ } else {
272
+ log(' Existing containers will not be stopped, restarted or recreated.')
273
+ log(' Restarting or unhealthy services may still fail when used.')
274
+ }
275
+ return true
276
+ } finally {
277
+ reader?.close()
278
+ }
279
+ }
280
+
129
281
  // WSL2 reports platform 'linux', but its usual Docker is Docker Desktop on the
130
282
  // Windows side, exposed to the distribution through "WSL integration". Telling
131
283
  // a WSL user to install Docker Engine would put a second engine next to it.
@@ -171,10 +323,20 @@ function remediationFor(problem, platform, { wsl = false, databaseUrlFile = '.en
171
323
  ]
172
324
  case 'port-conflict':
173
325
  return [
174
- ...problem.ports.map(({ service, port, envKey }) => `Port ${port} (${service}) is already in use by another process.${envKey ? ` Stop that process, or pick a free port with ${envKey} in .env.` : ' Stop that process.'}`),
175
- `If you change POSTGRES_PORT, update the port in DATABASE_URL (${databaseUrlFile}) as well.`,
176
- '`docker ps` shows containers of other projects that may hold these ports.',
177
- ...portConflictHints,
326
+ ...problem.ports.flatMap((entry) => [
327
+ `Port ${entry.port} (${diagnosticText(entry.service)}) is already in use.`,
328
+ ` ${listenerSummary(entry, diagnosticColors({ output: { isTTY: false } }))}${entry.containerName && entry.processes?.length ? `; container: ${diagnosticText(entry.containerName)}` : ''}`,
329
+ entry.envKey
330
+ ? entry.suggestedPort
331
+ ? ` Stop the listener above, or use a free port: set ${diagnosticText(entry.envKey)}=${entry.suggestedPort} in .env.`
332
+ : ` Stop the listener above, or pick a free port with ${diagnosticText(entry.envKey)} in .env.`
333
+ : ' Stop the listener above before retrying.',
334
+ '',
335
+ ]),
336
+ `If you change POSTGRES_PORT, update the port in DATABASE_URL (${databaseUrlFile}) as well${problem.ports.find((entry) => entry.envKey === 'POSTGRES_PORT' && entry.suggestedPort) ? ` (:${problem.ports.find((entry) => entry.envKey === 'POSTGRES_PORT').port} -> :${problem.ports.find((entry) => entry.envKey === 'POSTGRES_PORT').suggestedPort})` : ''}; REDIS_PORT likewise in REDIS_URL if you set one.`,
337
+ '`docker ps --all` shows existing containers, including restart loops, that may hold these ports.',
338
+ 'If this is an older Open Mercato run, run `yarn om stop` in that checkout before trying again.',
339
+ ...portConflictHints.flatMap((line) => diagnosticText(line).split(/(?<=\.) +(?=[A-Z])/)),
178
340
  ]
179
341
  case 'unhealthy':
180
342
  return [
@@ -192,6 +354,10 @@ function remediationFor(problem, platform, { wsl = false, databaseUrlFile = '.en
192
354
  }
193
355
  }
194
356
 
357
+ // Where a developer without Docker learns to run the services themselves.
358
+ export const WITHOUT_DOCKER_GUIDE_URL = 'https://github.com/open-mercato/open-mercato#without-docker'
359
+ export const DOCKER_RUNTIME_PROBLEM_CODES = new Set(['cli-missing', 'compose-missing', 'daemon-stopped', 'daemon-unresponsive', 'daemon-permission'])
360
+
195
361
  const PROBLEM_TITLES = {
196
362
  'cli-missing': 'Docker is not installed (docker CLI not found).',
197
363
  'compose-missing': 'Docker Compose v2 is not available.',
@@ -213,13 +379,34 @@ export function formatInfraProblem(problem, {
213
379
  wsl = platform === 'linux' && detectWsl(),
214
380
  databaseUrlFile = '.env',
215
381
  portConflictHints = [],
382
+ output = process.stdout,
383
+ env = process.env,
216
384
  } = {}) {
217
- return [
385
+ const colors = diagnosticColors({ output, env })
386
+ const lines = [
218
387
  PROBLEM_TITLES[problem.code] ?? PROBLEM_TITLES['compose-failed'],
219
- ...remediationFor(problem, platform, { wsl, databaseUrlFile, portConflictHints }).map((line) => ` ${line}`),
388
+ ...remediationFor(problem, platform, { wsl, databaseUrlFile, portConflictHints }).map((line) => line ? ` ${line}` : ''),
389
+ ...(DOCKER_RUNTIME_PROBLEM_CODES.has(problem.code)
390
+ ? ['', ` No Docker? Run PostgreSQL yourself (Redis and Meilisearch are optional): ${WITHOUT_DOCKER_GUIDE_URL}`]
391
+ : []),
220
392
  ` Already running PostgreSQL, Redis and Meilisearch yourself? Re-run with: ${optOutCommand}`,
221
393
  ' (it makes no Docker calls and keeps the URLs in your .env).',
222
394
  ]
395
+ return lines.map((line) => {
396
+ let decorated = diagnosticText(line)
397
+ .replace(/`([^`]+)`/g, (token) => colors.command(token))
398
+ .replace(/\b(?:[A-Z][A-Z0-9_]*_PORT|DATABASE_URL|MERCATO_STACK)\b/g, (token) => colors.variable(token))
399
+ for (const command of [optOutCommand, problem.logsCommand, 'yarn infra:down', 'sudo systemctl start docker'].filter(Boolean)) {
400
+ decorated = decorated.replaceAll(diagnosticText(command), colors.command(command))
401
+ }
402
+ for (const entry of problem.ports ?? problem.services ?? []) {
403
+ const service = diagnosticText(entry.service)
404
+ if (service) decorated = decorated.replaceAll(`(${service})`, `(${colors.service(service)})`)
405
+ if (entry.port) decorated = decorated.replaceAll(`Port ${entry.port}`, `Port ${colors.port(entry.port)}`)
406
+ for (const process of entry.processes ?? []) decorated = decorated.replaceAll(`PID: ${process.pid}`, `PID: ${colors.pid(process.pid)}`)
407
+ }
408
+ return decorated
409
+ })
223
410
  }
224
411
 
225
412
  // Preflight + `compose up -d --wait`. Never removes containers or volumes:
@@ -238,12 +425,22 @@ export async function startInfraServices({
238
425
  beforeUp,
239
426
  detect = detectDockerRuntime,
240
427
  spawnSyncImpl = spawnSync,
428
+ runStreamingImpl,
241
429
  portProbe = probeTcpPort,
430
+ containerProbe = findContainerPortOwners,
431
+ processProbe = findPortProcessOwners,
432
+ protectExistingServices = false,
242
433
  log = console.log,
243
434
  }) {
244
435
  const runtime = detect()
245
436
  if (!runtime.ok) return { ok: false, problem: { code: runtime.code, detail: runtime.detail } }
246
437
 
438
+ const attachProcesses = (entries) => entries.map((entry) => {
439
+ try {
440
+ const processes = processProbe(entry.port, { spawnSyncImpl })
441
+ return Array.isArray(processes) && processes.length > 0 ? { ...entry, processes } : entry
442
+ } catch { return entry }
443
+ })
247
444
  const required = services.map((entry) => entry.service)
248
445
  // null when `compose ps` itself fails (e.g. an interpolation error in .env).
249
446
  const listServices = () => {
@@ -253,21 +450,29 @@ export async function startInfraServices({
253
450
 
254
451
  const listed = listServices()
255
452
  const running = runningComposeServices(listed ?? [])
453
+ if (protectExistingServices && listed !== null) {
454
+ const existing = findUnhealthyServices(listed, required)
455
+ .filter((entry) => ['running', 'restarting'].includes(entry.state))
456
+ .map((entry) => ({ ...services.find((definition) => definition.service === entry.service), ...entry, containerName: listed.find((container) => container.Service === entry.service)?.Name }))
457
+ if (existing.length > 0) return { ok: false, problem: { code: 'unhealthy', services: attachProcesses(existing), existingServices: true } }
458
+ }
256
459
  // Without the project's container list our own running containers would look
257
460
  // like foreign port owners, so the port probe is skipped and `compose up`
258
461
  // reports the real compose error instead.
259
462
  if (listed === null) {
260
463
  log(' could not list this project\'s containers (docker compose ps failed); skipping the port check')
261
464
  } else {
262
- const conflicts = await findPortConflicts(services, { running, probe: portProbe })
263
- if (conflicts.length > 0) return { ok: false, problem: { code: 'port-conflict', ports: conflicts } }
465
+ const owners = containerProbe(services.filter((entry) => !running.has(entry.service)), { spawnSyncImpl }).filter((entry) => !running.has(entry.service))
466
+ const occupied = await findPortConflicts(services, { running, probe: portProbe })
467
+ const conflicts = services.filter((entry) => !running.has(entry.service) && (owners.some((owner) => owner.service === entry.service) || occupied.some((port) => port.service === entry.service))).map((entry) => owners.find((owner) => owner.service === entry.service) ?? entry)
468
+ if (conflicts.length > 0) return { ok: false, problem: { code: 'port-conflict', ports: await suggestFreePorts(attachProcesses(conflicts), { probe: portProbe, reserved: services.map((entry) => entry.port) }) } }
264
469
  }
265
470
 
266
471
  const reused = required.filter((service) => running.has(service))
267
472
  if (reused.length > 0) log(` reusing running containers: ${reused.join(', ')} (data preserved)`)
268
- if (beforeUp) beforeUp()
473
+ if (beforeUp) await beforeUp()
269
474
 
270
- const up = spawnSyncImpl('docker', composeArgs([...upArgs, 'up', '-d', '--wait']), { cwd, stdio: 'inherit', windowsHide: true })
475
+ const up = await (runStreamingImpl ?? spawnSyncImpl)('docker', composeArgs([...upArgs, 'up', '-d', '--wait']), { cwd, stdio: 'inherit', windowsHide: true })
271
476
  if (up?.error) return { ok: false, problem: { code: 'compose-failed', detail: up.error.message } }
272
477
  if ((up?.status ?? 1) === 0) return { ok: true, reused }
273
478
 
package/src/infra.mjs CHANGED
@@ -7,7 +7,7 @@ import fs from 'node:fs'
7
7
  import path from 'node:path'
8
8
  import { fileURLToPath } from 'node:url'
9
9
 
10
- import { composeArgs, detectDocker, resolveRepoRoot, runCompose, runCaptureSync } from './compose.mjs'
10
+ import { composeArgs, detectDocker, resolveRepoRoot, runCompose, runCaptureSync, runStreaming } from './compose.mjs'
11
11
  import { resolveStackPorts } from './constants.mjs'
12
12
  import { readEnvValue } from './env-file.mjs'
13
13
  import { formatInfraProblem, startInfraServices } from './infra-preflight.mjs'
@@ -62,7 +62,7 @@ export function externalDatabaseHost(repoRoot, env = process.env) {
62
62
  // infraUp plus the shared preflight: distinguishes missing CLI / compose,
63
63
  // stopped daemon, port conflicts, and failed healthchecks instead of a bare
64
64
  // non-zero compose exit. Returns { ok, problem? }; never touches volumes.
65
- export async function startInfra(repoRoot, { profiles = [], env = process.env, log = console.log, beforeUp, startImpl = startInfraServices } = {}) {
65
+ export async function startInfra(repoRoot, { profiles = [], env = process.env, log = console.log, logs, beforeUp, protectExistingServices = false, startImpl = startInfraServices } = {}) {
66
66
  ensureMcpSharedDir(repoRoot)
67
67
  const externalHost = externalDatabaseHost(repoRoot, env)
68
68
  if (externalHost) {
@@ -74,6 +74,8 @@ export async function startInfra(repoRoot, { profiles = [], env = process.env, l
74
74
  upArgs: profiles.flatMap((profile) => ['--profile', profile]),
75
75
  cwd: repoRoot,
76
76
  beforeUp,
77
+ protectExistingServices,
78
+ ...(logs ? { runStreamingImpl: async (command, args, options) => ({ status: await runStreaming(command, args, { ...options, env, logs }) }) } : {}),
77
79
  log,
78
80
  })
79
81
  }
package/src/providers.mjs CHANGED
@@ -80,13 +80,6 @@ function normalizeEnvTargets(rootEnvOrFiles) {
80
80
  return { rootEnv: rootEnvOrFiles.rootEnv, appEnv: rootEnvOrFiles.appEnv ?? null }
81
81
  }
82
82
 
83
- // Returns 'configured' | 'skipped' | 'failed'. Mirrors the semantics of the
84
- // Windows launcher's Invoke-LlmProviderPrompt: OM_AI_PROVIDER or an existing
85
- // key in .env means "already configured"; a key in the ambient environment is
86
- // persisted without prompting; otherwise prompt interactively unless
87
- // skip/non-interactive flags are set. Accepts the root .env path, or
88
- // { rootEnv, appEnv } to also keep the app env in sync (see
89
- // syncProviderConfigToAppEnv).
90
83
  export async function ensureLlmProvider(rootEnvOrFiles, { skipPrompt = false, nonInteractive = false, log = console.log, warn = console.warn } = {}) {
91
84
  const files = normalizeEnvTargets(rootEnvOrFiles)
92
85
  const rootEnv = files.rootEnv
@@ -115,32 +108,33 @@ export async function ensureLlmProvider(rootEnvOrFiles, { skipPrompt = false, no
115
108
  }
116
109
  }
117
110
 
118
- if (skipPrompt) {
119
- warn('LLM prompt skipped with no provider key — AI chat will not work until you set a provider (e.g. OPENAI_API_KEY) in .env and restart the opencode container.')
111
+ const skipProvider = () => {
112
+ log('')
113
+ warn('Continuing without AI. Configure a provider key in .env later and restart the opencode container to enable AI chat.')
120
114
  return 'skipped'
121
115
  }
122
- if (nonInteractive || !process.stdin.isTTY) {
123
- warn('No LLM provider API key found. Set a provider key (e.g. OPENAI_API_KEY) in the environment or .env, or pass --skip-llm-prompt to proceed without AI.')
124
- return 'failed'
125
- }
116
+ if (skipPrompt || nonInteractive || !process.stdin.isTTY) return skipProvider()
126
117
 
127
118
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
128
- const ask = (question) => new Promise((resolve) => rl.question(question, resolve))
119
+ const ask = (question) => new Promise((resolve) => rl.question(` ${question}`, resolve))
129
120
 
121
+ const print = (line) => console.log(line ? ` ${line}` : '')
130
122
  try {
131
- console.log('')
132
- console.log('The AI assistant (OpenCode + MCP) needs one LLM provider to power the Cmd+K agent.')
133
- console.log('Setup requires one to continue. (Ctrl+C aborts; re-run later with --skip-llm-prompt to configure it in .env yourself.)')
123
+ print('')
124
+ print('The AI assistant (OpenCode + MCP) needs one LLM provider to power the Cmd+K agent.')
125
+ print('AI setup is optional. Press Enter or choose 0 to continue without AI; you can configure it in .env later.')
134
126
 
135
127
  while (true) {
136
- console.log('')
128
+ print('')
129
+ print('[ 0] Skip for now (default)')
137
130
  LLM_PROVIDERS.forEach((entry, index) => {
138
- console.log(` [${String(index + 1).padStart(2)}] ${entry.label}`)
131
+ print(`[${String(index + 1).padStart(2)}] ${entry.label}`)
139
132
  })
140
- const choice = await ask(`Choose a provider [1-${LLM_PROVIDERS.length}]: `)
133
+ const choice = (await ask(`Choose a provider [0-${LLM_PROVIDERS.length}] (default 0): `)).trim()
134
+ if (!choice || choice === '0') return skipProvider()
141
135
  const index = Number.parseInt(choice, 10)
142
136
  if (!Number.isInteger(index) || index < 1 || index > LLM_PROVIDERS.length) {
143
- console.log(`Please enter a number between 1 and ${LLM_PROVIDERS.length}.`)
137
+ print(`Please enter a number between 0 and ${LLM_PROVIDERS.length}.`)
144
138
  continue
145
139
  }
146
140
  const selected = LLM_PROVIDERS[index - 1]
@@ -148,10 +142,7 @@ export async function ensureLlmProvider(rootEnvOrFiles, { skipPrompt = false, no
148
142
  let key = ''
149
143
  if (selected.keyRequired) {
150
144
  key = (await ask(`Paste your ${selected.label} API key: `)).trim()
151
- if (!key) {
152
- console.log('A key is required for this provider. Try again.')
153
- continue
154
- }
145
+ if (!key) return skipProvider()
155
146
  } else {
156
147
  key = (await ask(`${selected.label} API key (optional for local servers — press Enter to skip): `)).trim()
157
148
  }
@@ -164,7 +155,7 @@ export async function ensureLlmProvider(rootEnvOrFiles, { skipPrompt = false, no
164
155
  const entered = (await ask(promptText)).trim()
165
156
  baseUrl = entered || selected.baseUrl
166
157
  if (selected.baseUrlRequired && !baseUrl) {
167
- console.log('This provider requires a base URL. Try again.')
158
+ print('This provider requires a base URL. Try again.')
168
159
  continue
169
160
  }
170
161
  }
@@ -174,7 +165,7 @@ export async function ensureLlmProvider(rootEnvOrFiles, { skipPrompt = false, no
174
165
  : `${selected.label} model (${selected.modelHint}) (optional — press Enter for the default): `
175
166
  const model = (await ask(modelPrompt)).trim()
176
167
  if (selected.modelRequired && !model) {
177
- console.log('This provider needs a model/deployment id. Try again.')
168
+ print('This provider needs a model/deployment id. Try again.')
178
169
  continue
179
170
  }
180
171
 
@@ -0,0 +1,167 @@
1
+ import fs from 'node:fs'
2
+ import path from 'node:path'
3
+ import { createRequire } from 'node:module'
4
+ import { parseEnv } from 'node:util'
5
+
6
+ const DEFAULT_TIMEOUT_MS = 5000
7
+
8
+ function readSettings(projectDir, envFiles, env) {
9
+ const settings = {}
10
+ for (const file of [...envFiles, path.join(projectDir, '.env')]) {
11
+ try { Object.assign(settings, parseEnv(fs.readFileSync(file, 'utf8'))) }
12
+ catch (error) { if (error.code !== 'ENOENT') throw new Error('Cannot read service configuration. Check the permissions and syntax of the project .env files.') }
13
+ }
14
+ return Object.assign(settings, env)
15
+ }
16
+
17
+ async function loadClientModule(projectDir, name) {
18
+ const manifests = [path.join(projectDir, 'package.json'), path.join(projectDir, 'node_modules', '@open-mercato', 'cli', 'package.json'), path.join(projectDir, '..', '..', 'packages', 'cli', 'package.json')]
19
+ for (const manifest of manifests) {
20
+ try { return createRequire(manifest)(name) }
21
+ catch (error) { if (error.code !== 'MODULE_NOT_FOUND') throw error }
22
+ }
23
+ throw Object.assign(new Error('Service connection dependency is unavailable.'), { code: 'MISSING_DEPENDENCY' })
24
+ }
25
+
26
+ function endpoint(url) {
27
+ return `${url.protocol}//${url.host}${url.protocol.startsWith('postgres') ? url.pathname : ''}`
28
+ }
29
+
30
+ export class ServiceConnectionError extends Error {
31
+ constructor(service, envKey, target, kind) {
32
+ const advice = {
33
+ authentication: 'The service rejected the configured credentials or permissions. Check the username/password or API key and access rights in .env.',
34
+ unreachable: 'The service is unreachable or timed out. Start it and check the configured host, port, network, and TLS settings.',
35
+ dependency: 'Install project dependencies with yarn install, then retry setup.',
36
+ configuration: 'The service URL is missing or invalid. Check its value in .env.',
37
+ database: 'The configured PostgreSQL database does not exist. Create it or select the intended existing database in .env.',
38
+ connection: 'The service connection check failed. Check its configuration and server logs before retrying setup.',
39
+ }
40
+ super(`[setup] ${service} connection failed (${envKey}${target ? ` at ${target}` : ''}). ${advice[kind]}`)
41
+ this.name = 'ServiceConnectionError'
42
+ this.service = service
43
+ this.envKey = envKey
44
+ this.kind = kind
45
+ }
46
+ }
47
+
48
+ function failureKind(error) {
49
+ const code = error.code ?? error.cause?.code
50
+ if (error.code === 'MISSING_DEPENDENCY') return 'dependency'
51
+ if (error.code === '28P01' || error.code === '28000' || /WRONGPASS|NOAUTH|NOPERM|\bAUTH\b|invalid username-password/i.test(error.message ?? '')) return 'authentication'
52
+ if (error.code === '3D000') return 'database'
53
+ if (['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'ENOTFOUND', 'EHOSTUNREACH', 'CONNECTION_TIMEOUT'].includes(code) || ['TimeoutError', 'AbortError'].includes(error.name) || /timeout|timed out/i.test(error.message ?? '')) return 'unreachable'
54
+ return 'connection'
55
+ }
56
+
57
+ function parseTarget(raw, protocols, service, key) {
58
+ try {
59
+ const url = new URL(raw)
60
+ if (!protocols.includes(url.protocol) || !url.hostname) throw new Error('Invalid service URL')
61
+ return url
62
+ } catch { throw new ServiceConnectionError(service, key, '', 'configuration') }
63
+ }
64
+
65
+ async function bounded(operation, timeoutMs) {
66
+ let timer
67
+ try {
68
+ return await Promise.race([
69
+ Promise.resolve().then(operation),
70
+ new Promise((_, reject) => { timer = setTimeout(() => reject(Object.assign(new Error('Connection timed out'), { code: 'CONNECTION_TIMEOUT' })), timeoutMs) }),
71
+ ])
72
+ } finally { clearTimeout(timer) }
73
+ }
74
+
75
+ // A Redis URL that belongs to an optional feature is only verified when that
76
+ // feature is switched on: .env.example ships DOCUMENTS_COLLAB_REDIS_URL, but
77
+ // realtime collaboration stays off until NEXT_PUBLIC_DOCUMENTS_COLLAB_URL is set,
78
+ // so a machine without Redis must not fail setup on it.
79
+ const OPTIONAL_REDIS_KEYS = { DOCUMENTS_COLLAB_REDIS_URL: 'NEXT_PUBLIC_DOCUMENTS_COLLAB_URL' }
80
+
81
+ export function redisKeyInUse(key, settings) {
82
+ const enabledBy = OPTIONAL_REDIS_KEYS[key]
83
+ return !enabledBy || Boolean(settings[enabledBy]?.trim())
84
+ }
85
+
86
+ export async function verifyServiceConnections({ projectDir = process.cwd(), envFiles = [], env = process.env, timeoutMs = DEFAULT_TIMEOUT_MS, loadModule = loadClientModule, fetchImpl = fetch, log = console.log } = {}) {
87
+ if (!Number.isInteger(timeoutMs) || timeoutMs < 1) throw new Error('Service connection timeout must be a positive integer.')
88
+ const settings = readSettings(projectDir, envFiles, env)
89
+ const checked = []
90
+ const database = parseTarget(settings.DATABASE_URL, ['postgres:', 'postgresql:'], 'PostgreSQL', 'DATABASE_URL')
91
+ try {
92
+ const { Client } = await loadModule(projectDir, 'pg')
93
+ const probePostgres = async (target) => {
94
+ const client = new Client({ connectionString: target.toString(), connectionTimeoutMillis: timeoutMs, query_timeout: timeoutMs, statement_timeout: timeoutMs })
95
+ client.on?.('error', () => {})
96
+ try {
97
+ await bounded(() => client.connect(), timeoutMs)
98
+ await bounded(() => client.query('SELECT 1'), timeoutMs)
99
+ } finally {
100
+ try { await bounded(() => client.end(), Math.min(timeoutMs, 1000)) }
101
+ catch { client.connection?.stream?.destroy() }
102
+ }
103
+ }
104
+ try {
105
+ await probePostgres(database)
106
+ log(`[setup] PostgreSQL connection verified at ${endpoint(database)}.`)
107
+ } catch (error) {
108
+ if (error.code !== '3D000') throw error
109
+ const maintenance = new URL(database)
110
+ maintenance.pathname = '/postgres'
111
+ maintenance.searchParams.delete('database')
112
+ maintenance.searchParams.delete('dbname')
113
+ await probePostgres(maintenance)
114
+ log(`[setup] PostgreSQL credentials verified at ${endpoint(maintenance)}. The selected database does not exist yet; initialization will handle its creation.`)
115
+ }
116
+ checked.push('DATABASE_URL')
117
+ } catch (error) {
118
+ throw new ServiceConnectionError('PostgreSQL', 'DATABASE_URL', endpoint(database), failureKind(error))
119
+ }
120
+
121
+ const redisKeys = Object.keys(settings).filter((key) => (key === 'REDIS_URL' || key.endsWith('_REDIS_URL')) && settings[key]?.trim() && redisKeyInUse(key, settings))
122
+ for (const key of redisKeys) {
123
+ const target = parseTarget(settings[key], ['redis:', 'rediss:'], 'Redis', key)
124
+ let redis
125
+ let redisFailure
126
+ try {
127
+ const module = await loadModule(projectDir, 'ioredis')
128
+ const Redis = module.default ?? module.Redis ?? module
129
+ redis = new Redis(target.toString(), { lazyConnect: true, connectTimeout: timeoutMs, commandTimeout: timeoutMs, maxRetriesPerRequest: 0, enableOfflineQueue: false, retryStrategy: () => null })
130
+ redis.on('error', (error) => { redisFailure = error })
131
+ await bounded(() => redis.connect(), timeoutMs)
132
+ const reply = await bounded(() => redis.ping(), timeoutMs)
133
+ if (reply !== 'PONG') throw new Error('Unexpected Redis reply')
134
+ checked.push(key)
135
+ log(`[setup] Redis connection verified for ${key} at ${endpoint(target)}.`)
136
+ } catch (error) {
137
+ throw new ServiceConnectionError('Redis', key, endpoint(target), failureKind(redisFailure ?? error))
138
+ } finally { redis?.disconnect() }
139
+ }
140
+
141
+ if (settings.MEILISEARCH_HOST?.trim()) {
142
+ const target = parseTarget(settings.MEILISEARCH_HOST, ['http:', 'https:'], 'Meilisearch', 'MEILISEARCH_HOST')
143
+ try {
144
+ const api = new URL(target)
145
+ api.pathname = `${api.pathname.replace(/\/$/, '')}/indexes`
146
+ api.search = '?limit=1'
147
+ api.hash = ''
148
+ const key = settings.MEILISEARCH_API_KEY
149
+ const response = await bounded(() => fetchImpl(api, { method: 'GET', headers: key ? { Authorization: `Bearer ${key}` } : {}, signal: AbortSignal.timeout(timeoutMs) }), timeoutMs)
150
+ try {
151
+ if (response.status === 401 || response.status === 403) throw Object.assign(new Error('API key rejected'), { code: '28000' })
152
+ if (!response.ok) throw new Error('Protected API request failed')
153
+ const payload = await bounded(() => response.json(), timeoutMs)
154
+ if (!payload || !Array.isArray(payload.results)) throw new Error('Unexpected Meilisearch API response')
155
+ checked.push('MEILISEARCH_HOST')
156
+ log(`[setup] Meilisearch authenticated API connection verified at ${endpoint(target)}.`)
157
+ } finally {
158
+ if (response.body && !response.bodyUsed && !response.body.locked) {
159
+ try { await bounded(() => response.body.cancel(), Math.min(timeoutMs, 1000)) } catch {}
160
+ }
161
+ }
162
+ } catch (error) {
163
+ throw new ServiceConnectionError('Meilisearch', 'MEILISEARCH_HOST / MEILISEARCH_API_KEY', endpoint(target), failureKind(error))
164
+ }
165
+ }
166
+ return { ok: true, checked }
167
+ }