|
1 | 1 | import { cmd } from "./cmd" |
2 | 2 | import * as prompts from "./clack" |
3 | 3 | import { UI } from "../ui" |
4 | | -import { irisFetch, requireAuth, handleApiError, requireUserId, printDivider, printKV, dim, bold, success, highlight } from "./iris-api" |
| 4 | +import { irisFetch, requireAuth, handleApiError, requireUserId, printDivider, printKV, dim, bold, success, highlight, isNonInteractive } from "./iris-api" |
| 5 | +import { matchesSearchQuery } from "./bloq-item-format" |
5 | 6 | import { executeChat } from "./platform-chat" |
6 | 7 | import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs" |
7 | 8 | import { join } from "path" |
@@ -168,23 +169,73 @@ const AgentsListCommand = cmd({ |
168 | 169 | }, |
169 | 170 | }) |
170 | 171 |
|
| 172 | +/** |
| 173 | + * Resolve an agent ID from a numeric ID or a name (#162334). Mirrors the leads / |
| 174 | + * bloqs `get <name-or-id>` resolvers so a user who knows an agent's name but not |
| 175 | + * its ID has a path in. Filters the agents list client-side with the same |
| 176 | + * tokenized matcher used elsewhere. Returns the numeric ID, or null (having |
| 177 | + * printed the reason) on no/ambiguous match. |
| 178 | + */ |
| 179 | +async function resolveAgentId(idOrQuery: string | number, userId: number, json: boolean): Promise<number | null> { |
| 180 | + const numeric = Number(idOrQuery) |
| 181 | + if (Number.isInteger(numeric) && String(idOrQuery).trim() !== "") return numeric |
| 182 | + |
| 183 | + const query = String(idOrQuery) |
| 184 | + const res = await irisFetch(`/api/v1/users/${userId}/bloqs/agents?per_page=500`) |
| 185 | + if (!res.ok) { |
| 186 | + if (!json) prompts.log.error("Could not look up agents by name") |
| 187 | + process.exitCode = 1 |
| 188 | + return null |
| 189 | + } |
| 190 | + const raw = (await res.json()) as { data?: any[] } |
| 191 | + const matches = (raw?.data ?? []).filter((a) => matchesSearchQuery(String(a.name ?? ""), query)) |
| 192 | + |
| 193 | + if (matches.length === 0) { |
| 194 | + if (json) console.log(JSON.stringify({ error: `No agent matched "${query}"` }, null, 2)) |
| 195 | + else prompts.log.warn(`No agent matched "${query}" — try ${dim("iris agents list")}`) |
| 196 | + process.exitCode = 1 |
| 197 | + return null |
| 198 | + } |
| 199 | + if (matches.length === 1) return matches[0].id |
| 200 | + if (json || isNonInteractive()) { |
| 201 | + if (json) console.log(JSON.stringify({ error: "ambiguous", matches: matches.map((m) => ({ id: m.id, name: m.name })) }, null, 2)) |
| 202 | + else { |
| 203 | + prompts.log.warn(`${matches.length} agents match "${query}" — specify by ID:`) |
| 204 | + for (const m of matches) prompts.log.info(` #${m.id} ${m.name ?? "Unknown"}`) |
| 205 | + } |
| 206 | + process.exitCode = 1 |
| 207 | + return null |
| 208 | + } |
| 209 | + const choice = await prompts.select({ |
| 210 | + message: "Which agent?", |
| 211 | + options: matches.map((m) => ({ value: m.id, label: `#${m.id} ${m.name ?? "Unknown"}` })), |
| 212 | + }) |
| 213 | + if (prompts.isCancel(choice)) return null |
| 214 | + return choice as number |
| 215 | +} |
| 216 | + |
171 | 217 | const AgentsGetCommand = cmd({ |
172 | 218 | command: "get <id>", |
173 | | - describe: "show agent details", |
| 219 | + describe: "show agent details (accepts an agent ID or name)", |
174 | 220 | builder: (yargs) => |
175 | 221 | yargs |
176 | | - .positional("id", { describe: "agent ID", type: "number", demandOption: true }) |
| 222 | + .positional("id", { describe: "agent ID or name", type: "string", demandOption: true }) |
177 | 223 | .option("json", { describe: "JSON output", type: "boolean", default: false }) |
178 | 224 | .option("user-id", { describe: "user ID (or IRIS_USER_ID env)", type: "number" }), |
179 | 225 | async handler(args) { |
180 | | - if (!args.json) { UI.empty(); prompts.intro(`◈ Agent #${args.id}`) } |
| 226 | + if (!args.json) { UI.empty(); prompts.intro(`◈ Agent ${args.id}`) } |
181 | 227 |
|
182 | 228 | const token = await requireAuth() |
183 | 229 | if (!token) { if (!args.json) prompts.outro("Done"); return } |
184 | 230 |
|
185 | 231 | const userId = await requireUserId(args["user-id"]) |
186 | 232 | if (!userId) { if (!args.json) prompts.outro("Done"); return } |
187 | 233 |
|
| 234 | + // Resolve name → numeric ID (#162334). Numeric IDs pass straight through. |
| 235 | + const resolvedId = await resolveAgentId(args.id as any, userId, Boolean(args.json)) |
| 236 | + if (resolvedId === null) { if (!args.json) prompts.outro("Done"); return } |
| 237 | + args.id = resolvedId as any |
| 238 | + |
188 | 239 | const spinner = args.json ? null : prompts.spinner() |
189 | 240 | if (spinner) spinner.start("Loading…") |
190 | 241 |
|
|
0 commit comments