Unverified Commit 38c16c82 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

[ai-upgrade] Ask user to choose model and effort (#99235)

Stacked on #99232.

### Why?

Let users choose the model and effort for a new upgrade agent.

### How?

Ask for model, then effort, in separate selectors and pass both to the
CLI. Existing agents keep their settings.

<!-- NEXT_JS_LLM -->

Closes NAR-947
parent 2e2e5e06
......@@ -10,12 +10,68 @@ import { getAgentName } from '../../telemetry/agent-name'
import { bold, cyan, dim } from '../picocolors'
import { runChildProcess } from './run-child-process'
// Model defaults for newly launched sessions; existing agents keep their model.
const CODEX_EFFORTS = [
'low',
'medium',
'high',
'xhigh',
'max',
'ultra',
] as const
const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'max'] as const
const UPGRADE_MODELS = {
codex: 'gpt-5.6-terra',
claude: 'claude-sonnet-5[1m]',
codex: [
{ id: 'gpt-5.6-terra', label: 'GPT-5.6-Terra', efforts: CODEX_EFFORTS },
{ id: 'gpt-5.6-sol', label: 'GPT-5.6-Sol', efforts: CODEX_EFFORTS },
{ id: 'gpt-6-astra', label: 'GPT-6-Astra', efforts: CODEX_EFFORTS },
],
claude: [
{
id: 'claude-sonnet-5[1m]',
label: 'Claude Sonnet 5 (1M)',
efforts: CLAUDE_EFFORTS,
},
{ id: 'opus', label: 'Claude Opus (latest)', efforts: CLAUDE_EFFORTS },
{ id: 'fable', label: 'Claude Fable (latest)', efforts: CLAUDE_EFFORTS },
],
} as const
function getCodexModels(path: string) {
const result = spawn.sync(path, ['debug', 'models', '--bundled'], {
encoding: 'utf8',
timeout: 5000,
maxBuffer: 20 * 1024 * 1024,
})
if (result.status !== 0) {
Log.warn(
'Could not read the Codex model catalog; using the CLI defaults.',
result.error ?? (result.stderr?.trim() || `exit status ${result.status}`)
)
return []
}
try {
const catalog = JSON.parse(result.stdout) as {
models?: Array<{ slug: string }>
}
if (!Array.isArray(catalog.models)) {
Log.warn(
'Codex model catalog has an unexpected format; using the CLI defaults.'
)
return []
}
const available = new Set(catalog.models.map(({ slug }) => slug))
return UPGRADE_MODELS.codex.filter(({ id }) => available.has(id))
} catch (error) {
Log.warn(
'Could not parse the Codex model catalog; using the CLI defaults.',
error
)
return []
}
}
type UpgradeHarness = {
name: keyof typeof UPGRADE_MODELS
path: string
......@@ -30,6 +86,34 @@ function resolvePrompt(
return typeof prompt === 'string' ? prompt : prompt(useWorktree)
}
async function chooseOption(
question: string,
values: Record<string, string>,
defaultValue: number
): Promise<string | undefined> {
Log.bootstrap('')
Log.bootstrap(` ${question}`)
Log.bootstrap(` ${dim('Use ↑/↓ to choose, then press Enter.')}\n`)
try {
const { id } = await cliSelect({
values: { ...values, cancel: 'Cancel' },
defaultValue,
selected: cyan('❯'),
unselected: ' ',
indentation: 2,
valueRenderer: (value: string, selected: boolean) =>
selected ? cyan(bold(value)) : value,
})
return typeof id === 'string' && id !== 'cancel' ? id : undefined
} catch (error) {
if (error) {
throw error
}
return undefined
}
}
async function chooseWorktree(): Promise<boolean> {
Log.bootstrap(' Open the upgrade in a separate Git worktree?')
const { id } = await cliSelect({
......@@ -72,7 +156,20 @@ async function findHarnesses(): Promise<UpgradeHarness[]> {
if ((await stat(file)).isFile()) {
return { name, path: file }
}
} catch {}
} catch (error) {
const code = (error as NodeJS.ErrnoException).code
if (
code === 'ENOENT' ||
code === 'EACCES' ||
code === 'ENOTDIR' ||
code === 'ELOOP'
) {
continue
}
throw new Error(`Could not inspect coding agent at ${file}.`, {
cause: error,
})
}
}
}
......@@ -170,25 +267,35 @@ function copyUpgradePrompt(prompt: string, noHarness = false): void {
function launchHarness(
harness: UpgradeHarness,
prompt: string,
directory: string
directory: string,
model: string | null,
effort: string
): Promise<number> {
// Windows shell shims cannot carry literal line breaks in an argument.
if (process.platform === 'win32' && /\.(cmd|bat)$/i.test(harness.path)) {
prompt = prompt.replace(/[\r\n]+/g, ' ')
}
return runChildProcess(
harness.path,
['--model', UPGRADE_MODELS[harness.name], prompt],
{ cwd: directory, stdio: 'inherit' }
)
const args = model === null ? [] : ['--model', model]
if (effort !== 'default') {
if (harness.name === 'codex') {
args.push('-c', `model_reasoning_effort=${effort}`)
} else {
args.push('--effort', effort)
}
}
args.push(prompt)
return runChildProcess(harness.path, args, {
cwd: directory,
stdio: 'inherit',
})
}
export async function handoffUpgrade(
prompt: UpgradePrompt,
directory: string
): Promise<void> {
// Existing agents keep their session, model and permissions.
// Existing agents keep their session and permissions.
if (await getAgentName()) {
Log.bootstrap(resolvePrompt(prompt, null))
return
......@@ -222,6 +329,51 @@ export async function handoffUpgrade(
return
}
const models =
harness.name === 'codex'
? getCodexModels(harness.path)
: UPGRADE_MODELS.claude
if (models.length === 0 && harness.name !== 'codex') {
Log.error('No supported models were found for the selected coding agent.')
process.exitCode = 1
return
}
let model: (typeof models)[number] | undefined
let effort = 'default'
if (models.length === 0) {
Log.warn('Could not verify Codex models; using the CLI defaults.')
} else {
const modelId = await chooseOption(
`Which ${getHarnessDisplayName(harness.name)} model should run the upgrade?`,
Object.fromEntries(models.map(({ id, label }) => [id, label])),
0
)
model = models.find(({ id }) => id === modelId)
if (!model) {
Log.bootstrap(` ${dim('Upgrade cancelled.')}\n`)
process.exitCode = 1
return
}
const selectedEffort = await chooseOption(
'Which reasoning effort should the upgrade use?',
{
default: 'Model default',
...Object.fromEntries(model.efforts.map((value) => [value, value])),
},
0
)
if (
!selectedEffort ||
(selectedEffort !== 'default' &&
!model.efforts.some((value) => value === selectedEffort))
) {
Log.bootstrap(` ${dim('Upgrade cancelled.')}\n`)
process.exitCode = 1
return
}
effort = selectedEffort
}
let useWorktree: boolean
try {
useWorktree = await chooseWorktree()
......@@ -240,7 +392,9 @@ export async function handoffUpgrade(
process.exitCode = await launchHarness(
harness,
resolvePrompt(prompt, useWorktree),
directory
directory,
model?.id ?? null,
effort
)
} catch {
Log.error(`Could not start ${getHarnessDisplayName(harness.name)}.`)
......
This diff is collapsed.
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