The Open Standard for Generative UI
Start and open CLI apps in interactive mode (#973)
Zahle Khanc60329ac329b
diff --git a/docs/content/docs/api-reference/cli.mdx b/docs/content/docs/api-reference/cli.mdxindex 5dfd4b1f4..604424d16 100644--- a/docs/content/docs/api-reference/cli.mdx+++ b/docs/content/docs/api-reference/cli.mdx@@ -67,9 +67,11 @@ openui create [options]| `--no-interactive` | Fail instead of prompting for missing input || `--agent-name <name>` | Declare the invoking coding-agent slug (default: `unknown`) |-When run interactively (default), the CLI prompts for any missing options, then asks whether to start the development server after installing dependencies with the detected package manager. The start prompt defaults to yes; answering no preserves the existing install-and-exit behavior and prints the `cd` and dev commands. For most prototypes and evaluations, start with **OpenUI Cloud, the recommended default**: hosted models, managed conversation history and streaming, built-in tools, and ready-to-use reports and presentations without operating the model, storage, or artifact infrastructure. Choose **self-hosted** when owning the OpenAI-compatible provider, AI route, and persistence is a requirement.+When run interactively (default), the CLI prompts for any missing options, installs dependencies with the detected package manager, starts the development server, and opens its local URL in the default browser. There is no separate start-server prompt; pass `--no-immediate` to install and exit instead. For most prototypes and evaluations, start with **OpenUI Cloud, the recommended default**: hosted models, managed conversation history and streaming, built-in tools, and ready-to-use reports and presentations without operating the model, storage, or artifact infrastructure. Choose **self-hosted** when owning the OpenAI-compatible provider, AI route, and persistence is a requirement.-In non-interactive mode, dependencies are installed without starting the long-running development server. Pass `--immediate` to install and start, `--no-immediate` to make the install-and-exit behavior explicit, or `--no-install` to scaffold only.+In non-interactive mode, dependencies are installed without starting the long-running development server. Pass `--immediate` to install, start, and open the app, or `--no-install` to scaffold only.++`--immediate` and `--no-immediate` are mutually exclusive; passing both exits with an error.**What it does**diff --git a/packages/openui-cli/README.md b/packages/openui-cli/README.mdindex 22466ab1e..2dc57f3f7 100644--- a/packages/openui-cli/README.md+++ b/packages/openui-cli/README.md@@ -75,15 +75,17 @@ Options:- `--auth <method>`: (cloud template) How to obtain the key — `oauth` or `skip`; `manual` remains available for backward compatibility but is deprecated- `--agent-name <name>`: Declare the invoking coding agent as a lowercase kebab-case product slug (default: `unknown`)+`--immediate` and `--no-immediate` are mutually exclusive; passing both exits with an error.+What it does:- prompts for the project name, defaulting to `openui-agent`, if you do not pass `--name`- prompts for the template if you do not pass `--template`- copies the bundled template into a new directory- rewrites monorepo-local dependencies (`workspace:`, `file:`, `catalog:`) in the generated `package.json` to `latest`-- asks whether to start the development server after dependency installation (default: yes); answering no preserves the install-and-exit behavior- installs dependencies automatically using the detected package manager (unless `--no-install`)-- in non-interactive mode, installs dependencies without starting the server unless overridden with `--immediate`+- in interactive sessions, starts the development server and opens its local URL in the default browser; pass `--no-immediate` to install and exit instead+- in non-interactive sessions, installs and exits unless `--immediate` is passed- optionally installs the OpenUI agent skill for AI coding assistants- writes a `.env` file tailored to the template (see below)diff --git a/packages/openui-cli/package.json b/packages/openui-cli/package.jsonindex ebe28c8dd..e03eb2716 100644--- a/packages/openui-cli/package.json+++ b/packages/openui-cli/package.json@@ -56,6 +56,7 @@"commander": "^14.0.3","cross-spawn": "^7.0.6","esbuild": "^0.25.10",+ "get-port": "^7.2.0","open": "^10.1.0","openid-client": "^6.1.7","posthog-node": "^5.35.6"diff --git a/packages/openui-cli/src/commands/create-app.ts b/packages/openui-cli/src/commands/create-app.tsindex 3e0f7db09..848fa1f87 100644--- a/packages/openui-cli/src/commands/create-app.ts+++ b/packages/openui-cli/src/commands/create-app.ts@@ -9,6 +9,7 @@ import {resolveInstallPackageManager,type PackageManagerName,} from "../lib/detect-package-manager";+import { runDevCommand } from "../lib/dev-server";import { runSkillInstall, shouldInstallSkill } from "../lib/install-skill";import { runCommand } from "../lib/process-runner";import { resolveArgs } from "../lib/resolve-args";@@ -199,11 +200,7 @@ export async function runCreateApp(options: CreateAppOptions): Promise<void> {skill_installed: installSkill,});- const immediateResolution = await resolveImmediate(- options.immediate,- options.noInstall,- interactive,- );+ const immediateResolution = resolveImmediate(options.immediate, options.noInstall, interactive);telemetry.capture("cli_immediate_selected", {immediate: immediateResolution.immediate,dependency_install_requested: immediateResolution.installDependencies,@@ -432,7 +429,7 @@ export async function runCreateApp(options: CreateAppOptions): Promise<void> {telemetry.capture("cli_dev_command_started", {package_manager: packageManager.name,});- const devResult = await runCommand(devCmd, ["run", "dev"], targetDir);+ const devResult = await runDevCommand(devCmd, targetDir);const stoppedNormally =devResult.status === 0 ||devResult.status === 130 ||@@ -467,15 +464,15 @@ export async function runCreateApp(options: CreateAppOptions): Promise<void> {const isInteractiveTerminal = () => Boolean(process.stdin.isTTY && process.stdout.isTTY);-async function resolveImmediate(+function resolveImmediate(immediate: boolean | undefined,noInstall: boolean | undefined,interactive: boolean,-): Promise<{+): {immediate: boolean;installDependencies: boolean;- source: "flag" | "interactive_prompt" | "no_install" | "noninteractive_default";-}> {+ source: "flag" | "interactive_default" | "no_install" | "noninteractive_default";+} {if (noInstall) {return { immediate: false, installDependencies: false, source: "no_install" };}@@ -493,23 +490,7 @@ async function resolveImmediate(source: "noninteractive_default",};}-- try {- const { confirm } = await import("@inquirer/prompts");- const selected = await confirm({- message: "Start dev server after install?",- default: true,- });- return {- immediate: selected,- installDependencies: true,- source: "interactive_prompt",- };- } catch (error) {- const { ExitPromptError } = await import("@inquirer/core");- if (error instanceof ExitPromptError) throw new CliCancelledError("immediate_prompt");- throw error;- }+ return { immediate: true, installDependencies: true, source: "interactive_default" };}async function writeEnv(targetDir: string, result: EnvResult, appId?: string): Promise<void> {diff --git a/packages/openui-cli/src/index.ts b/packages/openui-cli/src/index.tsindex 9e18f4ac3..17a09fcd1 100644--- a/packages/openui-cli/src/index.ts+++ b/packages/openui-cli/src/index.ts@@ -9,7 +9,7 @@ import { Command } from "commander";import { runCreateApp } from "./commands/create-app";import { GenerateOptions, runGenerate } from "./commands/generate";import { detectAgent, UNKNOWN_AGENT_NAME } from "./lib/detect-agent";-import { resolveArgs } from "./lib/resolve-args";+import { rejectConflictingImmediateFlags, resolveArgs } from "./lib/resolve-args";import { telemetry } from "./lib/telemetry";import { handleCliError, normalizeAuth, normalizeTemplate } from "./lib/utils"; // Ensure utils.ts is included for type declarations@@ -89,6 +89,7 @@ Templates:immediate?: boolean;}) => {try {+ rejectConflictingImmediateFlags(process.argv.slice(2));await runCreateApp({name: options.name,template: normalizeTemplate(options.template),diff --git a/packages/openui-cli/src/lib/dev-server.ts b/packages/openui-cli/src/lib/dev-server.tsnew file mode 100644index 000000000..e1e1e50e3--- /dev/null+++ b/packages/openui-cli/src/lib/dev-server.ts@@ -0,0 +1,47 @@+import * as http from "node:http";++import { runCommand, type CommandResult } from "./process-runner";++function isServerReady(url: string): Promise<boolean> {+ return new Promise((resolve) => {+ const request = http.get(url, { timeout: 1_000 }, (response) => {+ response.resume();+ resolve(true);+ });+ request.once("timeout", () => request.destroy());+ request.once("error", () => resolve(false));+ });+}++async function openWhenReady(url: string, didExit: () => boolean): Promise<void> {+ // A free port is not necessarily accepting connections yet. Wait for the dev+ // server to respond so the browser does not open to a connection-refused page.+ const timeoutAt = Date.now() + 60_000;+ while (!didExit() && Date.now() < timeoutAt) {+ if (await isServerReady(url)) {+ console.info(`\n\ud83c\udf10 Opening ${url} in your browser...`);+ try {+ const { default: open } = await import("open");+ await open(url);+ } catch {+ console.info(`Could not open a browser automatically. Visit ${url}`);+ }+ return;+ }+ await new Promise((resolve) => setTimeout(resolve, 250));+ }+}++export async function runDevCommand(command: string, cwd: string): Promise<CommandResult> {+ const { default: getPort, portNumbers } = await import("get-port");+ const port = await getPort({ port: portNumbers(3000, 3100) });+ const url = `http://localhost:${port}`;+ let exited = false;+ const resultPromise = runCommand(command, ["run", "dev"], cwd, {+ env: { ...process.env, PORT: String(port) },+ });+ void openWhenReady(url, () => exited);+ const result = await resultPromise;+ exited = true;+ return result;+}diff --git a/packages/openui-cli/src/lib/process-runner.ts b/packages/openui-cli/src/lib/process-runner.tsindex d3a160dd1..4559ecb69 100644--- a/packages/openui-cli/src/lib/process-runner.ts+++ b/packages/openui-cli/src/lib/process-runner.ts@@ -10,6 +10,10 @@ export type CommandResult = {diagnosticTail: string;};+export type RunCommandOptions = {+ env?: NodeJS.ProcessEnv;+};+/*** `spawn.sync(..., { stdio: "inherit" })` exposes exit status but not the output* needed to distinguish dependency, workspace, and package-compatibility failures.@@ -19,11 +23,17 @@ export type CommandResult = {* Running asynchronously also lets the parent forward SIGINT/SIGTERM, wait for the* child to close, and return cancellation metadata before telemetry is flushed.*/-export function runCommand(command: string, args: string[], cwd: string): Promise<CommandResult> {+export function runCommand(+ command: string,+ args: string[],+ cwd: string,+ options: RunCommandOptions = {},+): Promise<CommandResult> {return new Promise((resolve) => {const startedAt = Date.now();const child = spawn(command, args, {cwd,+ env: options.env,stdio: ["inherit", "pipe", "pipe"],});let diagnosticTail = "";diff --git a/packages/openui-cli/src/lib/resolve-args.ts b/packages/openui-cli/src/lib/resolve-args.tsindex 565e936f3..89ab681e5 100644--- a/packages/openui-cli/src/lib/resolve-args.ts+++ b/packages/openui-cli/src/lib/resolve-args.ts@@ -20,6 +20,16 @@ type ResolvedArgs<T extends Record<string, ArgDef<unknown>>> = {[K in keyof T]: T[K] extends { value: infer V } ? V : string;};+export function rejectConflictingImmediateFlags(args: string[]): void {+ const separatorIndex = args.indexOf("--");+ const optionArgs = separatorIndex === -1 ? args : args.slice(0, separatorIndex);+ const hasImmediate = optionArgs.some((arg) => arg === "--immediate" || arg === "-i");+ const hasNoImmediate = optionArgs.includes("--no-immediate");+ if (hasImmediate && hasNoImmediate) {+ throw new CreateError("bad_args", "--immediate and --no-immediate cannot be used together.");+ }+}+async function resolveOne(prompt: PromptConfig): Promise<string> {const { input, select } = await import("@inquirer/prompts");if (prompt.type === "select") {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex f3d6965bc..83d01db02 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1874,6 +1874,9 @@ importers:esbuild:specifier: ^0.25.10version: 0.25.12+ get-port:+ specifier: ^7.2.0+ version: 7.2.0open:specifier: ^10.1.0version: 10.2.0@@ -12826,6 +12829,10 @@ packages:resolution: {integrity: sha512-I9QVvBw5U/hw3RmWpYKRumUeaDgxTPd401x364rLmWBJcOQ753eov1eTgzDqRG9bqFIfDc7gfzcQEWrUri3o1A==}+ resolution: {integrity: sha512-afP4W205ONCuMoPBqcR6PSXnzX35KTcJygfJfcp+QY+uwm3p20p1YczWXhlICIzGMCxYBQcySEcOgsJcrkyobg==}+ engines: {node: '>=16'}+resolution: {integrity: sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==}engines: {node: '>= 0.4'}@@ -32458,6 +32465,8 @@ snapshots:+ [email protected]: {}+dependencies:dunder-proto: 1.0.1