refactor: deduplicate resolveAgentKey/resolveCloudKey into shared resolveEntityKey (#481)

Agent: complexity-hunter

Co-authored-by: A <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
A 2026-02-11 09:11:41 -08:00 committed by GitHub
parent 5851a9ce7b
commit 7fa9325683
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -137,37 +137,30 @@ export function findClosestKeyByNameOrKey(
}
/**
* Resolve user input to a valid agent key.
* Resolve user input to a valid entity key (agent or cloud).
* Tries: exact key -> case-insensitive key -> display name match (case-insensitive).
* Returns the key if found, or null.
*/
export function resolveAgentKey(manifest: Manifest, input: string): string | null {
if (manifest.agents[input]) return input;
function resolveEntityKey(manifest: Manifest, input: string, kind: "agent" | "cloud"): string | null {
const collection = getEntityCollection(manifest, kind);
if (collection[input]) return input;
const keys = getEntityKeys(manifest, kind);
const lower = input.toLowerCase();
for (const key of agentKeys(manifest)) {
for (const key of keys) {
if (key.toLowerCase() === lower) return key;
}
for (const key of agentKeys(manifest)) {
if (manifest.agents[key].name.toLowerCase() === lower) return key;
for (const key of keys) {
if (collection[key].name.toLowerCase() === lower) return key;
}
return null;
}
/**
* Resolve user input to a valid cloud key.
* Tries: exact key -> case-insensitive key -> display name match (case-insensitive).
* Returns the key if found, or null.
*/
export function resolveAgentKey(manifest: Manifest, input: string): string | null {
return resolveEntityKey(manifest, input, "agent");
}
export function resolveCloudKey(manifest: Manifest, input: string): string | null {
if (manifest.clouds[input]) return input;
const lower = input.toLowerCase();
for (const key of cloudKeys(manifest)) {
if (key.toLowerCase() === lower) return key;
}
for (const key of cloudKeys(manifest)) {
if (manifest.clouds[key].name.toLowerCase() === lower) return key;
}
return null;
return resolveEntityKey(manifest, input, "cloud");
}
interface EntityDef { label: string; labelPlural: string; listCmd: string; opposite: string }