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:
| `--skip-install` | Explicitly tell the CLI to skip installing packages |
| `--disable-git` | Explicitly tell the CLI to disable git initialization |
| `--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 |
## Examples
......@@ -88,7 +90,7 @@ On installation, you'll see the following prompts:
```txt filename="Terminal"
What is your project named? my-app
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, customize settings - Choose your own preferences
```
......@@ -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
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 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.
......
......@@ -45,6 +45,7 @@ export async function createApp({
reactCompiler,
cacheComponents,
agentsMd,
agentFeedback,
}: {
appPath: string
packageManager: PackageManager
......@@ -65,6 +66,7 @@ export async function createApp({
reactCompiler: boolean
cacheComponents: boolean
agentsMd: boolean
agentFeedback: boolean
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
......@@ -258,6 +260,7 @@ export async function createApp({
bundler,
reactCompiler,
cacheComponents,
agentFeedback,
})
}
......
......@@ -111,6 +111,14 @@ const program = new Command(packageJson.name)
'--agents-md',
'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.`)
.action((name) => {
// Commander does not implicitly support negated options. When they are used
......@@ -232,6 +240,7 @@ async function run(): Promise<void> {
*/
let skipPrompt = ciInfo.isCI || opts.yes
let useRecommendedDefaults = false
let enableAgentFeedbackByDefault = false
if (!example) {
const defaults: typeof preferences = {
......@@ -250,8 +259,13 @@ async function run(): Promise<void> {
agentsMd: true,
}
const recommendedDefaults = {
...defaults,
agentFeedback: true,
}
type DisplayConfigItem = {
key: keyof typeof defaults
key: keyof typeof defaults | 'agentFeedback'
values?: Record<string, string>
flags?: Record<string, string>
}
......@@ -297,6 +311,10 @@ async function run(): Promise<void> {
values: { 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
......@@ -329,6 +347,8 @@ async function run(): Promise<void> {
// --typescript --tailwind --app and expect the rest to use sensible defaults
// without entering interactive mode.
const hasProvidedOptions = process.argv.some((arg) => arg.startsWith('--'))
const shouldPromptForAgentFeedback =
!ciInfo.isCI && !opts.yes && !hasProvidedOptions
if (!skipPrompt && hasProvidedOptions) {
skipPrompt = true
......@@ -347,7 +367,7 @@ async function run(): Promise<void> {
{
title: 'Yes, use recommended defaults',
value: 'recommended',
description: formatSettingsDescription(defaults),
description: formatSettingsDescription(recommendedDefaults),
},
{
title: 'No, customize settings',
......@@ -384,6 +404,7 @@ async function run(): Promise<void> {
if (setupChoice === 'recommended') {
useRecommendedDefaults = true
enableAgentFeedbackByDefault = recommendedDefaults.agentFeedback
skipPrompt = true
} else if (setupChoice === 'reuse') {
skipPrompt = true
......@@ -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
// defaults that were assumed so agents and users know what to override.
if (hasProvidedOptions && useRecommendedDefaults) {
......@@ -717,6 +766,15 @@ async function run(): Promise<void> {
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
const hasImportAlias = process.argv.some(
(arg) =>
......@@ -760,6 +818,7 @@ async function run(): Promise<void> {
reactCompiler: opts.reactCompiler,
cacheComponents: opts.cacheComponents,
agentsMd: opts.agentsMd,
agentFeedback: opts.agentFeedback,
})
} catch (reason) {
if (!(reason instanceof DownloadError)) {
......@@ -796,6 +855,7 @@ async function run(): Promise<void> {
reactCompiler: opts.reactCompiler,
cacheComponents: opts.cacheComponents,
agentsMd: opts.agentsMd,
agentFeedback: opts.agentFeedback,
})
}
conf.set('preferences', preferences)
......
......@@ -61,6 +61,7 @@ export const installTemplate = async ({
bundler,
reactCompiler,
cacheComponents,
agentFeedback,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
......@@ -169,6 +170,21 @@ export const installTemplate = async ({
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(
root,
mode === "js" ? "jsconfig.json" : "tsconfig.json",
......
......@@ -34,6 +34,7 @@ export interface InstallTemplateArgs {
bundler: Bundler;
reactCompiler: boolean;
cacheComponents: boolean;
agentFeedback: boolean;
}
export enum Bundler {
......
import { mkdir, writeFile } from 'fs/promises'
import { mkdir, readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import {
resolveNextTgzFilename,
......@@ -15,6 +15,18 @@ describe('create-next-app', () => {
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 () => {
await useTempDir(async (cwd) => {
const projectName = 'non-empty-dir'
......@@ -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 () => {
await useTempDir(async (cwd) => {
const projectName = 'partial-flags'
......@@ -189,6 +232,7 @@ describe('create-next-app', () => {
--no-src-dir No src/ directory (use --src-dir for src/ directory)
--no-cache-components No Cache Components (use --cache-components for Cache Components)
--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 "@/*""
`)
})
......@@ -210,6 +254,7 @@ describe('create-next-app', () => {
'--no-react-compiler',
'--no-cache-components',
'--no-agents-md',
'--no-agent-feedback',
'--skip-install',
...(process.env.NEXT_RSPACK ? ['--rspack'] : []),
],
......@@ -221,6 +266,9 @@ describe('create-next-app', () => {
)
expect(res.exitCode).toBe(0)
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', () => {
],
}
`)
expect(
readFileSync(join(cwd, projectName, 'next.config.ts'), 'utf8')
).not.toContain('agentFeedback')
})
})
......@@ -236,8 +239,16 @@ describe('create-next-app prompts', () => {
)
await new Promise<void>((resolve) => {
let output = ''
childProcess.stdout.on('data', (data) => {
output += data
process.stdout.write(data)
})
childProcess.on('exit', async (exitCode) => {
expect(exitCode).toBe(0)
expect(output).toContain('Agent feedback')
expect(output).not.toMatch(/agents prepare anonymized feedback/)
projectFilesShouldExist({
cwd,
projectName,
......@@ -259,6 +270,9 @@ describe('create-next-app prompts', () => {
const pkg = require(join(cwd, projectName, 'package.json'))
expect(pkg.name).toBe(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', () => {
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) => {
expect(exitCode).toBe(0)
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