|
| 1 | +/** |
| 2 | + * List agents command |
| 3 | + */ |
| 4 | + |
| 5 | +import chalk from "chalk"; |
| 6 | +import { listAgents, type Agent } from "../../services/agentService.js"; |
| 7 | +import { output, outputError } from "../../utils/output.js"; |
| 8 | +import { formatTimeAgo } from "../../utils/time.js"; |
| 9 | + |
| 10 | +interface ListOptions { |
| 11 | + full?: boolean; |
| 12 | + name?: string; |
| 13 | + search?: string; |
| 14 | + public?: boolean; |
| 15 | + private?: boolean; |
| 16 | + output?: string; |
| 17 | +} |
| 18 | + |
| 19 | +// Column widths (NAME is dynamic, takes remaining space) |
| 20 | +const COL_VERSION = 14; |
| 21 | +const COL_VISIBILITY = 10; |
| 22 | +const COL_ID = 30; |
| 23 | +const COL_CREATED = 10; |
| 24 | +const FIXED_WIDTH = COL_VERSION + COL_VISIBILITY + COL_ID + COL_CREATED + 4; // 4 for spacing |
| 25 | + |
| 26 | +function truncate(str: string, maxLen: number): string { |
| 27 | + if (str.length <= maxLen) return str; |
| 28 | + return str.slice(0, maxLen - 1) + "…"; |
| 29 | +} |
| 30 | + |
| 31 | +function printTable(agents: Agent[]): void { |
| 32 | + if (agents.length === 0) { |
| 33 | + console.log(chalk.dim("No agents found")); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const termWidth = process.stdout.columns || 120; |
| 38 | + const nameWidth = Math.max(10, termWidth - FIXED_WIDTH); |
| 39 | + |
| 40 | + // Header |
| 41 | + const header = |
| 42 | + "NAME".padEnd(nameWidth) + |
| 43 | + " " + |
| 44 | + "VERSION".padEnd(COL_VERSION) + |
| 45 | + " " + |
| 46 | + "VISIBILITY".padEnd(COL_VISIBILITY) + |
| 47 | + " " + |
| 48 | + "ID".padEnd(COL_ID) + |
| 49 | + " " + |
| 50 | + "CREATED".padEnd(COL_CREATED); |
| 51 | + console.log(chalk.bold(header)); |
| 52 | + console.log(chalk.dim("─".repeat(Math.min(header.length, termWidth)))); |
| 53 | + |
| 54 | + for (const agent of agents) { |
| 55 | + const name = truncate(agent.name, nameWidth).padEnd(nameWidth); |
| 56 | + const version = truncate(agent.version, COL_VERSION).padEnd(COL_VERSION); |
| 57 | + const visibility = (agent.is_public ? "public" : "private").padEnd( |
| 58 | + COL_VISIBILITY, |
| 59 | + ); |
| 60 | + const visibilityColored = agent.is_public |
| 61 | + ? chalk.green(visibility) |
| 62 | + : chalk.dim(visibility); |
| 63 | + const id = truncate(agent.id, COL_ID).padEnd(COL_ID); |
| 64 | + const created = formatTimeAgo(agent.create_time_ms).padEnd(COL_CREATED); |
| 65 | + |
| 66 | + console.log( |
| 67 | + `${name} ${version} ${visibilityColored} ${chalk.dim(id)} ${chalk.dim(created)}`, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + console.log(); |
| 72 | + console.log( |
| 73 | + chalk.dim(`${agents.length} agent${agents.length !== 1 ? "s" : ""}`), |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Keep only the most recently created agent for each name. |
| 79 | + */ |
| 80 | +function keepLatestPerName(agents: Agent[]): Agent[] { |
| 81 | + const latestByName = new Map<string, Agent>(); |
| 82 | + for (const agent of agents) { |
| 83 | + const existing = latestByName.get(agent.name); |
| 84 | + if (!existing || agent.create_time_ms > existing.create_time_ms) { |
| 85 | + latestByName.set(agent.name, agent); |
| 86 | + } |
| 87 | + } |
| 88 | + return Array.from(latestByName.values()); |
| 89 | +} |
| 90 | + |
| 91 | +export async function listAgentsCommand(options: ListOptions): Promise<void> { |
| 92 | + try { |
| 93 | + const result = await listAgents({ |
| 94 | + publicOnly: options.public, |
| 95 | + privateOnly: options.private, |
| 96 | + name: options.name, |
| 97 | + search: options.search, |
| 98 | + }); |
| 99 | + |
| 100 | + const agents = options.full |
| 101 | + ? result.agents |
| 102 | + : keepLatestPerName(result.agents); |
| 103 | + |
| 104 | + const format = options.output || "text"; |
| 105 | + if (format !== "text") { |
| 106 | + output(agents, { format, defaultFormat: "json" }); |
| 107 | + } else { |
| 108 | + printTable(agents); |
| 109 | + } |
| 110 | + } catch (error) { |
| 111 | + outputError("Failed to list agents", error); |
| 112 | + } |
| 113 | +} |
0 commit comments