Unverified Commit ce3fe455 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

Add agent feedback to create-next-app defaults (#99119)

## Summary

- include experimental agent feedback in the interactive recommended
defaults
- ask about agent feedback only when someone chooses a non-default
interactive setup, with **Yes** selected by default
- generate `experimental.agentFeedback: true` when enabled and support
explicit `--agent-feedback` and `--no-agent-feedback` flags
- keep CI, `--yes`, and other non-interactive flows opted out unless
`--agent-feedback` is provided
- document and test the recommended and customized setup flows

## CLI experience

Choosing the recommended defaults enables agent feedback without another
prompt:

```text
? Would you like to use the recommended Next.js defaults?
❯ Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, No Cache Components, AGENTS.md, Agent feedback
  No, customize settings - Choose your own preferences
```

Choosing a non-default setup asks:

```text
? Would you like to help improve Next.js by letting agents prepare anonymized feedback for your review as you code? (Disable anytime with `experimental.agentFeedback: false`.) › No / Yes
```

**Yes** is selected by default.

## Verification

- `pnpm --filter=create-next-app build`
- `HEADLESS=true pnpm --config.minimum-release-age=0 test-start-turbo
test/production/create-next-app/prompts.test.ts -t "should use
recommended defaults|should show reuse previous settings|should not
prompt user for choice and use defaults if --yes is defined"`
- `HEADLESS=true pnpm --config.minimum-release-age=0 test-start-turbo
test/production/create-next-app/index.test.ts -t "should list both agent
feedback flags in help|should enable agent feedback with
--agent-feedback|should not print assumed defaults when all flags are
provided"`
- Next.js watch build and type checking completed with no errors
- Prettier and ESLint passed

<!-- NEXT_JS_LLM -->
parent 56fca738
...@@ -59,6 +59,8 @@ The following options are available: ...@@ -59,6 +59,8 @@ The following options are available:
| `--skip-install` | Explicitly tell the CLI to skip installing packages | | `--skip-install` | Explicitly tell the CLI to skip installing packages |
| `--disable-git` | Explicitly tell the CLI to disable git initialization | | `--disable-git` | Explicitly tell the CLI to disable git initialization |
| `--agents-md` | Include `AGENTS.md` to guide coding agents (default) | | `--agents-md` | Include `AGENTS.md` to guide coding agents (default) |
| `--agent-feedback` | Prepare anonymized Next.js feedback for your review |
| `--no-agent-feedback` | Do not prepare anonymized Next.js feedback for your review |
| `--yes` | Use previous preferences or defaults for all options | | `--yes` | Use previous preferences or defaults for all options |
## Examples ## Examples
...@@ -88,7 +90,7 @@ On installation, you'll see the following prompts: ...@@ -88,7 +90,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal" ```txt filename="Terminal"
What is your project named? my-app What is your project named? my-app
Would you like to use the recommended Next.js defaults? Would you like to use the recommended Next.js defaults?
Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, No Cache Components, AGENTS.md Yes, use recommended defaults - TypeScript, ESLint, No React Compiler, Tailwind CSS, No src/ directory, App Router, No Cache Components, AGENTS.md, Agent feedback
No, reuse previous settings No, reuse previous settings
No, customize settings - Choose your own preferences No, customize settings - Choose your own preferences
``` ```
...@@ -106,6 +108,7 @@ Would you like to use Cache Components? No / Yes ...@@ -106,6 +108,7 @@ Would you like to use Cache Components? No / Yes
Would you like to customize the import alias (`@/*` by default)? No / Yes Would you like to customize the import alias (`@/*` by default)? No / Yes
What import alias would you like configured? @/* What import alias would you like configured? @/*
Would you like to include AGENTS.md to guide coding agents to write up-to-date Next.js code? No / Yes Would you like to include AGENTS.md to guide coding agents to write up-to-date Next.js code? No / Yes
Would you like to help improve Next.js by letting agents prepare anonymized feedback for your review as you code? (Disable anytime with `experimental.agentFeedback: false`.) No / Yes
``` ```
After the prompts, `create-next-app` will create a folder with your project name and install the required dependencies. After the prompts, `create-next-app` will create a folder with your project name and install the required dependencies.
......
...@@ -45,6 +45,7 @@ export async function createApp({ ...@@ -45,6 +45,7 @@ export async function createApp({
reactCompiler, reactCompiler,
cacheComponents, cacheComponents,
agentsMd, agentsMd,
agentFeedback,
}: { }: {
appPath: string appPath: string
packageManager: PackageManager packageManager: PackageManager
...@@ -65,6 +66,7 @@ export async function createApp({ ...@@ -65,6 +66,7 @@ export async function createApp({
reactCompiler: boolean reactCompiler: boolean
cacheComponents: boolean cacheComponents: boolean
agentsMd: boolean agentsMd: boolean
agentFeedback: boolean
}): Promise<void> { }): Promise<void> {
let repoInfo: RepoInfo | undefined let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js' const mode: TemplateMode = typescript ? 'ts' : 'js'
...@@ -258,6 +260,7 @@ export async function createApp({ ...@@ -258,6 +260,7 @@ export async function createApp({
bundler, bundler,
reactCompiler, reactCompiler,
cacheComponents, cacheComponents,
agentFeedback,
}) })
} }
......
...@@ -111,6 +111,14 @@ const program = new Command(packageJson.name) ...@@ -111,6 +111,14 @@ const program = new Command(packageJson.name)
'--agents-md', '--agents-md',
'Include AGENTS.md to guide coding agents to write up-to-date Next.js code. (default)' 'Include AGENTS.md to guide coding agents to write up-to-date Next.js code. (default)'
) )
.option(
'--agent-feedback',
'Prepare anonymized Next.js feedback for your review.'
)
.option(
'--no-agent-feedback',
'Do not prepare anonymized Next.js feedback for your review.'
)
.option('--disable-git', `Skip initializing a git repository.`) .option('--disable-git', `Skip initializing a git repository.`)
.action((name) => { .action((name) => {
// Commander does not implicitly support negated options. When they are used // Commander does not implicitly support negated options. When they are used
...@@ -232,6 +240,7 @@ async function run(): Promise<void> { ...@@ -232,6 +240,7 @@ async function run(): Promise<void> {
*/ */
let skipPrompt = ciInfo.isCI || opts.yes let skipPrompt = ciInfo.isCI || opts.yes
let useRecommendedDefaults = false let useRecommendedDefaults = false
let enableAgentFeedbackByDefault = false
if (!example) { if (!example) {
const defaults: typeof preferences = { const defaults: typeof preferences = {
...@@ -250,8 +259,13 @@ async function run(): Promise<void> { ...@@ -250,8 +259,13 @@ async function run(): Promise<void> {
agentsMd: true, agentsMd: true,
} }
const recommendedDefaults = {
...defaults,
agentFeedback: true,
}
type DisplayConfigItem = { type DisplayConfigItem = {
key: keyof typeof defaults key: keyof typeof defaults | 'agentFeedback'
values?: Record<string, string> values?: Record<string, string>
flags?: Record<string, string> flags?: Record<string, string>
} }
...@@ -297,6 +311,10 @@ async function run(): Promise<void> { ...@@ -297,6 +311,10 @@ async function run(): Promise<void> {
values: { true: 'AGENTS.md', false: 'No AGENTS.md' }, values: { true: 'AGENTS.md', false: 'No AGENTS.md' },
flags: { true: '--agents-md', false: '--no-agents-md' }, flags: { true: '--agents-md', false: '--no-agents-md' },
}, },
{
key: 'agentFeedback',
values: { true: 'Agent feedback', false: 'No agent feedback' },
},
] ]
// Helper to format settings for display based on displayConfig // Helper to format settings for display based on displayConfig
...@@ -329,6 +347,8 @@ async function run(): Promise<void> { ...@@ -329,6 +347,8 @@ async function run(): Promise<void> {
// --typescript --tailwind --app and expect the rest to use sensible defaults // --typescript --tailwind --app and expect the rest to use sensible defaults
// without entering interactive mode. // without entering interactive mode.
const hasProvidedOptions = process.argv.some((arg) => arg.startsWith('--')) const hasProvidedOptions = process.argv.some((arg) => arg.startsWith('--'))
const shouldPromptForAgentFeedback =
!ciInfo.isCI && !opts.yes && !hasProvidedOptions
if (!skipPrompt && hasProvidedOptions) { if (!skipPrompt && hasProvidedOptions) {
skipPrompt = true skipPrompt = true
...@@ -347,7 +367,7 @@ async function run(): Promise<void> { ...@@ -347,7 +367,7 @@ async function run(): Promise<void> {
{ {
title: 'Yes, use recommended defaults', title: 'Yes, use recommended defaults',
value: 'recommended', value: 'recommended',
description: formatSettingsDescription(defaults), description: formatSettingsDescription(recommendedDefaults),
}, },
{ {
title: 'No, customize settings', title: 'No, customize settings',
...@@ -384,6 +404,7 @@ async function run(): Promise<void> { ...@@ -384,6 +404,7 @@ async function run(): Promise<void> {
if (setupChoice === 'recommended') { if (setupChoice === 'recommended') {
useRecommendedDefaults = true useRecommendedDefaults = true
enableAgentFeedbackByDefault = recommendedDefaults.agentFeedback
skipPrompt = true skipPrompt = true
} else if (setupChoice === 'reuse') { } else if (setupChoice === 'reuse') {
skipPrompt = true skipPrompt = true
...@@ -686,6 +707,34 @@ async function run(): Promise<void> { ...@@ -686,6 +707,34 @@ async function run(): Promise<void> {
} }
} }
if (opts.agentFeedback === false) {
opts.agentFeedback = false
} else if (!opts.agentFeedback) {
if (enableAgentFeedbackByDefault) {
opts.agentFeedback = true
} else if (shouldPromptForAgentFeedback) {
const { agentFeedback } = await prompts(
{
type: 'toggle',
name: 'agentFeedback',
message: `Would you like to help improve Next.js by letting agents prepare ${blue('anonymized feedback')} for your review as you code? (Disable anytime with \`experimental.agentFeedback: false\`.)`,
initial: true,
active: 'Yes',
inactive: 'No',
},
{
onCancel: () => {
console.error('Exiting.')
process.exit(1)
},
}
)
opts.agentFeedback = Boolean(agentFeedback)
} else {
opts.agentFeedback = false
}
}
// When prompts were skipped because flags were provided, print the // When prompts were skipped because flags were provided, print the
// defaults that were assumed so agents and users know what to override. // defaults that were assumed so agents and users know what to override.
if (hasProvidedOptions && useRecommendedDefaults) { if (hasProvidedOptions && useRecommendedDefaults) {
...@@ -717,6 +766,15 @@ async function run(): Promise<void> { ...@@ -717,6 +766,15 @@ async function run(): Promise<void> {
lines.push(` ${flag.padEnd(24)}${label}${altText}`) lines.push(` ${flag.padEnd(24)}${label}${altText}`)
} }
const hasAgentFeedback = process.argv.some(
(arg) => arg === '--agent-feedback' || arg === '--no-agent-feedback'
)
if (!hasAgentFeedback) {
lines.push(
` ${'--no-agent-feedback'.padEnd(24)}No agent feedback (use --agent-feedback for Agent feedback)`
)
}
// Import alias is not a boolean toggle, handle separately // Import alias is not a boolean toggle, handle separately
const hasImportAlias = process.argv.some( const hasImportAlias = process.argv.some(
(arg) => (arg) =>
...@@ -760,6 +818,7 @@ async function run(): Promise<void> { ...@@ -760,6 +818,7 @@ async function run(): Promise<void> {
reactCompiler: opts.reactCompiler, reactCompiler: opts.reactCompiler,
cacheComponents: opts.cacheComponents, cacheComponents: opts.cacheComponents,
agentsMd: opts.agentsMd, agentsMd: opts.agentsMd,
agentFeedback: opts.agentFeedback,
}) })
} catch (reason) { } catch (reason) {
if (!(reason instanceof DownloadError)) { if (!(reason instanceof DownloadError)) {
...@@ -796,6 +855,7 @@ async function run(): Promise<void> { ...@@ -796,6 +855,7 @@ async function run(): Promise<void> {
reactCompiler: opts.reactCompiler, reactCompiler: opts.reactCompiler,
cacheComponents: opts.cacheComponents, cacheComponents: opts.cacheComponents,
agentsMd: opts.agentsMd, agentsMd: opts.agentsMd,
agentFeedback: opts.agentFeedback,
}) })
} }
conf.set('preferences', preferences) conf.set('preferences', preferences)
......
...@@ -61,6 +61,7 @@ export const installTemplate = async ({ ...@@ -61,6 +61,7 @@ export const installTemplate = async ({
bundler, bundler,
reactCompiler, reactCompiler,
cacheComponents, cacheComponents,
agentFeedback,
}: InstallTemplateArgs) => { }: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`)); console.log(bold(`Using ${packageManager}.`));
...@@ -169,6 +170,21 @@ export const installTemplate = async ({ ...@@ -169,6 +170,21 @@ export const installTemplate = async ({
await fs.writeFile(nextConfigFile, configContent); await fs.writeFile(nextConfigFile, configContent);
} }
if (agentFeedback) {
const nextConfigFile = path.join(
root,
mode === "js" ? "next.config.mjs" : "next.config.ts",
);
let configContent = await fs.readFile(nextConfigFile, "utf8");
configContent = configContent.replace(
"/* config options here */\n",
"/* config options here */\n experimental: {\n agentFeedback: true,\n },\n",
);
await fs.writeFile(nextConfigFile, configContent);
}
const tsconfigFile = path.join( const tsconfigFile = path.join(
root, root,
mode === "js" ? "jsconfig.json" : "tsconfig.json", mode === "js" ? "jsconfig.json" : "tsconfig.json",
......
...@@ -34,6 +34,7 @@ export interface InstallTemplateArgs { ...@@ -34,6 +34,7 @@ export interface InstallTemplateArgs {
bundler: Bundler; bundler: Bundler;
reactCompiler: boolean; reactCompiler: boolean;
cacheComponents: boolean; cacheComponents: boolean;
agentFeedback: boolean;
} }
export enum Bundler { export enum Bundler {
......
import { mkdir, writeFile } from 'fs/promises' import { mkdir, readFile, writeFile } from 'fs/promises'
import { join } from 'path' import { join } from 'path'
import { import {
resolveNextTgzFilename, resolveNextTgzFilename,
...@@ -15,6 +15,18 @@ describe('create-next-app', () => { ...@@ -15,6 +15,18 @@ describe('create-next-app', () => {
nextTgzFilename = resolveNextTgzFilename() nextTgzFilename = resolveNextTgzFilename()
}) })
it('should list both agent feedback flags in help', async () => {
await useTempDir(async (cwd) => {
const res = await run(['--help'], nextTgzFilename, {
cwd,
stdio: 'pipe',
})
expect(res.stdout).toContain('--agent-feedback')
expect(res.stdout).toContain('--no-agent-feedback')
})
})
it('should not create if the target directory is not empty', async () => { it('should not create if the target directory is not empty', async () => {
await useTempDir(async (cwd) => { await useTempDir(async (cwd) => {
const projectName = 'non-empty-dir' const projectName = 'non-empty-dir'
...@@ -157,6 +169,37 @@ describe('create-next-app', () => { ...@@ -157,6 +169,37 @@ describe('create-next-app', () => {
}) })
}) })
it('should enable agent feedback with --agent-feedback', async () => {
await useTempDir(async (cwd) => {
const projectName = 'with-agent-feedback'
const res = await run(
[
projectName,
'--ts',
'--app',
'--no-linter',
'--no-tailwind',
'--no-src-dir',
'--no-import-alias',
'--no-react-compiler',
'--no-agents-md',
'--agent-feedback',
'--skip-install',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
nextTgzFilename,
{
cwd,
}
)
expect(res.exitCode).toBe(0)
expect(
await readFile(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('\n experimental: {\n agentFeedback: true,\n },\n')
})
})
it('should print assumed defaults when flags are partially provided', async () => { it('should print assumed defaults when flags are partially provided', async () => {
await useTempDir(async (cwd) => { await useTempDir(async (cwd) => {
const projectName = 'partial-flags' const projectName = 'partial-flags'
...@@ -189,6 +232,7 @@ describe('create-next-app', () => { ...@@ -189,6 +232,7 @@ describe('create-next-app', () => {
--no-src-dir No src/ directory (use --src-dir for src/ directory) --no-src-dir No src/ directory (use --src-dir for src/ directory)
--no-cache-components No Cache Components (use --cache-components for Cache Components) --no-cache-components No Cache Components (use --cache-components for Cache Components)
--agents-md AGENTS.md (use --no-agents-md for No AGENTS.md) --agents-md AGENTS.md (use --no-agents-md for No AGENTS.md)
--no-agent-feedback No agent feedback (use --agent-feedback for Agent feedback)
--import-alias "@/*"" --import-alias "@/*""
`) `)
}) })
...@@ -210,6 +254,7 @@ describe('create-next-app', () => { ...@@ -210,6 +254,7 @@ describe('create-next-app', () => {
'--no-react-compiler', '--no-react-compiler',
'--no-cache-components', '--no-cache-components',
'--no-agents-md', '--no-agents-md',
'--no-agent-feedback',
'--skip-install', '--skip-install',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []), ...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
], ],
...@@ -221,6 +266,9 @@ describe('create-next-app', () => { ...@@ -221,6 +266,9 @@ describe('create-next-app', () => {
) )
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect(res.stdout).not.toContain('Using defaults for unprovided options') expect(res.stdout).not.toContain('Using defaults for unprovided options')
expect(
await readFile(join(cwd, projectName, 'next.config.ts'), 'utf8')
).not.toContain('agentFeedback')
}) })
}) })
......
...@@ -221,6 +221,9 @@ describe('create-next-app prompts', () => { ...@@ -221,6 +221,9 @@ describe('create-next-app prompts', () => {
], ],
} }
`) `)
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).not.toContain('agentFeedback')
}) })
}) })
...@@ -236,8 +239,16 @@ describe('create-next-app prompts', () => { ...@@ -236,8 +239,16 @@ describe('create-next-app prompts', () => {
) )
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
let output = ''
childProcess.stdout.on('data', (data) => {
output += data
process.stdout.write(data)
})
childProcess.on('exit', async (exitCode) => { childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0) expect(exitCode).toBe(0)
expect(output).toContain('Agent feedback')
expect(output).not.toMatch(/agents prepare anonymized feedback/)
projectFilesShouldExist({ projectFilesShouldExist({
cwd, cwd,
projectName, projectName,
...@@ -259,6 +270,9 @@ describe('create-next-app prompts', () => { ...@@ -259,6 +270,9 @@ describe('create-next-app prompts', () => {
const pkg = require(join(cwd, projectName, 'package.json')) const pkg = require(join(cwd, projectName, 'package.json'))
expect(pkg.name).toBe(projectName) expect(pkg.name).toBe(projectName)
expectTurbopackTailwindSetup(cwd, projectName) expectTurbopackTailwindSetup(cwd, projectName)
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).toContain('\n experimental: {\n agentFeedback: true,\n },\n')
}) })
}) })
...@@ -304,6 +318,12 @@ describe('create-next-app prompts', () => { ...@@ -304,6 +318,12 @@ describe('create-next-app prompts', () => {
expect(output).toMatch(/No, reuse previous settings/) expect(output).toMatch(/No, reuse previous settings/)
}) })
await retry(async () => {
expect(output).toMatch(/agents prepare anonymized feedback/)
})
// Accept the default "Yes" for agent feedback.
childProcess.stdin.write('\n')
childProcess.on('exit', async (exitCode) => { childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0) expect(exitCode).toBe(0)
projectFilesShouldExist({ projectFilesShouldExist({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment