Skip to content

Commit 92ceb4b

Browse files
mayoalexanderclaude
andcommitted
feat(bloqs,agents): get accepts a name or id (#162334)
leads get resolves a name/email, but bloqs get and agents get were ID-only — the exact wall that forced users into the broken bloqs search. Port the leads name-or-id resolver: numeric args pass straight through, names are matched client-side with the tokenized matcher bloqs search uses (0 -> not-found exit 1, 1 -> resolve, many -> ambiguity list / prompt, never a guess). Verified live: 'bloqs get "Mayo Life Atlas"' now resolves #544 'MAYO — Life Atlas'. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 59ed8be commit 92ceb4b

2 files changed

Lines changed: 109 additions & 7 deletions

File tree

‎packages/opencode/src/cli/cmd/platform-agents.ts‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { cmd } from "./cmd"
22
import * as prompts from "./clack"
33
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"
56
import { executeChat } from "./platform-chat"
67
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs"
78
import { join } from "path"
@@ -168,23 +169,73 @@ const AgentsListCommand = cmd({
168169
},
169170
})
170171

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+
171217
const AgentsGetCommand = cmd({
172218
command: "get <id>",
173-
describe: "show agent details",
219+
describe: "show agent details (accepts an agent ID or name)",
174220
builder: (yargs) =>
175221
yargs
176-
.positional("id", { describe: "agent ID", type: "number", demandOption: true })
222+
.positional("id", { describe: "agent ID or name", type: "string", demandOption: true })
177223
.option("json", { describe: "JSON output", type: "boolean", default: false })
178224
.option("user-id", { describe: "user ID (or IRIS_USER_ID env)", type: "number" }),
179225
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}`) }
181227

182228
const token = await requireAuth()
183229
if (!token) { if (!args.json) prompts.outro("Done"); return }
184230

185231
const userId = await requireUserId(args["user-id"])
186232
if (!userId) { if (!args.json) prompts.outro("Done"); return }
187233

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+
188239
const spinner = args.json ? null : prompts.spinner()
189240
if (spinner) spinner.start("Loading…")
190241

‎packages/opencode/src/cli/cmd/platform-bloqs.ts‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,27 +177,78 @@ const BloqsListCommand = cmd({
177177
},
178178
})
179179

180+
/**
181+
* Resolve a bloq ID from a numeric ID or a name (#162334). Mirrors the leads
182+
* `get <name-or-id>` resolver so users who know a bloq's name but not its ID
183+
* have a path in. The bloqs index returns all of a user's bloqs, so we filter
184+
* client-side with the same tokenized matcher `bloqs search` uses. Returns the
185+
* numeric ID, or null (already having printed the reason) on no/ambiguous match.
186+
*/
187+
async function resolveBloqId(idOrQuery: string | number, userId: number, json: boolean): Promise<number | null> {
188+
const numeric = Number(idOrQuery)
189+
if (Number.isInteger(numeric) && String(idOrQuery).trim() !== "") return numeric
190+
191+
const query = String(idOrQuery)
192+
const res = await irisFetch(`/api/v1/user/${userId}/bloqs?simplified=1&per_page=500`)
193+
if (!res.ok) {
194+
if (!json) prompts.log.error("Could not look up bloqs by name")
195+
process.exitCode = 1
196+
return null
197+
}
198+
const data = (await res.json()) as { data?: any[] }
199+
const matches = (data?.data ?? []).filter((b) => matchesSearchQuery(String(b.name ?? ""), query))
200+
201+
if (matches.length === 0) {
202+
if (json) console.log(JSON.stringify({ error: `No bloq matched "${query}"` }, null, 2))
203+
else prompts.log.warn(`No bloq matched "${query}" — try ${dim("iris bloqs list")}`)
204+
process.exitCode = 1
205+
return null
206+
}
207+
if (matches.length === 1) return matches[0].id
208+
// Ambiguous — never guess. List candidates (non-interactive) or prompt.
209+
if (json || isNonInteractive()) {
210+
if (json) console.log(JSON.stringify({ error: "ambiguous", matches: matches.map((m) => ({ id: m.id, name: m.name })) }, null, 2))
211+
else {
212+
prompts.log.warn(`${matches.length} bloqs match "${query}" — specify by ID:`)
213+
for (const m of matches) prompts.log.info(` #${m.id} ${m.name ?? "Unknown"}`)
214+
}
215+
process.exitCode = 1
216+
return null
217+
}
218+
const choice = await prompts.select({
219+
message: "Which bloq?",
220+
options: matches.map((m) => ({ value: m.id, label: `#${m.id} ${m.name ?? "Unknown"}` })),
221+
})
222+
if (prompts.isCancel(choice)) return null
223+
return choice as number
224+
}
225+
180226
const BloqsGetCommand = cmd({
181227
command: "get <id>",
182-
describe: "show bloq details and lists",
228+
describe: "show bloq details and lists (accepts a bloq ID or name)",
183229
builder: (yargs) =>
184230
yargs
185-
.positional("id", { describe: "bloq ID", type: "number", demandOption: true })
231+
.positional("id", { describe: "bloq ID or name", type: "string", demandOption: true })
186232
.option("json", { describe: "JSON output", type: "boolean", default: false })
187233
.option("files", { describe: "list files attached to this bloq", type: "boolean", default: false })
188234
.option("items", { describe: "show recent items across all lists", type: "boolean", default: false })
189235
.option("list", { describe: "show items in a specific list (by ID)", type: "number" })
190236
.option("limit", { describe: "max items to show (default 10)", type: "number", default: 10 })
191237
.option("user-id", { describe: "user ID (or IRIS_USER_ID env)", type: "number" }),
192238
async handler(args) {
193-
if (!args.json) { UI.empty(); prompts.intro(`◈ Bloq #${args.id}`) }
239+
if (!args.json) { UI.empty(); prompts.intro(`◈ Bloq ${args.id}`) }
194240

195241
const token = await requireAuth()
196242
if (!token) { if (!args.json) prompts.outro("Done"); return }
197243

198244
const userId = await requireUserId(args["user-id"])
199245
if (!userId) { if (!args.json) prompts.outro("Done"); return }
200246

247+
// Resolve name → numeric ID (#162334). Numeric IDs pass straight through.
248+
const resolvedId = await resolveBloqId(args.id as any, userId, Boolean(args.json))
249+
if (resolvedId === null) { if (!args.json) prompts.outro("Done"); return }
250+
args.id = resolvedId as any
251+
201252
const spinner = args.json ? null : prompts.spinner()
202253
if (spinner) spinner.start("Loading…")
203254

0 commit comments

Comments
 (0)