feat(cli): add 'gemini models list' with JSON output - #29404
Kaushik2210 wants to merge 1 commit into
Conversation
Integrations that wrap the CLI have no programmatic way to discover which models are valid for -m/--model; the interactive /model dialog can't be parsed, so callers hardcode model IDs that go stale. Add a `models list` subcommand (following the `skills list` pattern) that prints the available models as text, or as JSON with `-o json` (currentModel plus id, name, description and inputTokenLimit per model). It reuses buildAvailableModels(), the same function ACP mode uses, so the list can't drift from what /model shows and covers both the legacy and dynamic model configuration paths. Fixes google-gemini#27847
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request adds a new Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
|
📊 PR Size: size/L
|
There was a problem hiding this comment.
Code Review
This pull request introduces the gemini models list command to list available models in either text or JSON format, along with corresponding documentation and unit tests. The review feedback highlights two key improvement opportunities: handling potential undefined values from tokenLimit and m.name to prevent TypeScript compilation errors under strictNullChecks, and wrapping the subcommand handler in a try/catch block to gracefully handle errors and ensure a clean terminal exit.
| const models: ListedModel[] = availableModels.map((m) => ({ | ||
| id: m.modelId, | ||
| name: m.name, | ||
| description: m.description ?? '', | ||
| inputTokenLimit: tokenLimit(m.modelId), | ||
| })); |
There was a problem hiding this comment.
The tokenLimit function can return undefined if a model's limit is not defined or if the model is unrecognized. Since inputTokenLimit in the ListedModel interface is strictly typed as a number, assigning a potentially undefined value will cause a TypeScript compilation error under strictNullChecks. Additionally, m.name may be optional in the model interface and should have a fallback to prevent potential runtime issues.
| const models: ListedModel[] = availableModels.map((m) => ({ | |
| id: m.modelId, | |
| name: m.name, | |
| description: m.description ?? '', | |
| inputTokenLimit: tokenLimit(m.modelId), | |
| })); | |
| const models: ListedModel[] = availableModels.map((m) => ({ | |
| id: m.modelId, | |
| name: m.name ?? m.modelId, | |
| description: m.description ?? '', | |
| inputTokenLimit: tokenLimit(m.modelId) ?? 0, | |
| })); |
References
- When consuming an object, if a property is optional in its type definition (interface), callers must handle the
undefinedcase (e.g., by providing a default with??). Do not rely on the implementation details of the function that creates the object to always provide a value, as this can change. Code against the interface contract.
| handler: async (argv) => { | ||
| await handleList({ | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| outputFormat: argv['outputFormat'] as ModelsListOutputFormat, | ||
| }); | ||
| await exitCli(); | ||
| }, |
There was a problem hiding this comment.
If handleList throws an error (e.g., due to configuration initialization failure or network issues), the command handler will fail with an unhandled promise rejection. This can cause the CLI to crash abruptly without restoring the terminal state or performing proper cleanup. Wrapping the execution in a try/catch block and calling exitCli(1) ensures a graceful exit and clean terminal state restoration.
handler: async (argv) => {
try {
await handleList({
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
outputFormat: argv['outputFormat'] as ModelsListOutputFormat,
});
await exitCli();
} catch (error) {
console.error(chalk.red(error instanceof Error ? error.message : String(error)));
await exitCli(1);
}
},|
Hi maintainers, this PR implements #27847 ( |
Summary
Adds a
gemini models listsubcommand so integrations can discover which models are valid for-m/--modelwithout hardcoding IDs that go stale. The interactive/modeldialog can't be parsed by external tools.Without
-o jsonit prints a human-readable list and marks the current model.Design notes
skills listpattern: amodels <command>parent registered viadefer(), withlistas its subcommand, and-o/--output-format text|jsonmatching the existing global flag's spelling.buildAvailableModels()(the function ACP mode uses) rather than reimplementing selection logic, so this list can't drift from what/modelshows, and it covers both the legacy andexperimental.dynamicModelConfigurationpaths.gemini models list -o json | jqworks.inputTokenLimitis reported, from the existingtokenLimit()helper. The issue also mentions output limits, but the codebase has no source for those, so I didn't invent one.skills/hooks/mcp, a new top-level subcommand means a prompt whose first word is literallymodelsis now parsed as the command. I kept the surface small by not adding amodelalias.Test plan
packages/cli/src/commands/models.test.tsandmodels/list.test.ts(7 tests): JSON shape, JSON-only stdout, text output with current-model marker, and yargs option wiring.gemini models list -o jsonreturns valid JSON (checked by parsing stdout with stderr discarded), text mode renders correctly,-o xmlis rejected by yargs, and baregemini modelsprints help.npx eslinton all touched files andnpx tsc --noEmitonpackages/cli: clean.docs/cli/cli-reference.md.Fixes #27847