|
| 1 | +import { defineCommand, detectOutputFormat, type FlagsDef } from "bailian-cli-core"; |
| 2 | +import { emitBare, emitResult, formatTable } from "bailian-cli-runtime"; |
| 3 | +import { listSkills } from "@openagentpack/sdk"; |
| 4 | +import { buildAgentRuntime, CREDENTIALS_NOTE } from "./_engine/config-loader.ts"; |
| 5 | +import { withStdoutProtected } from "./_engine/console-capture.ts"; |
| 6 | +import { withAgentErrors } from "./_engine/errors.ts"; |
| 7 | + |
| 8 | +const SKILL_SOURCES = ["custom", "official", "all"] as const; |
| 9 | +type SkillSource = (typeof SKILL_SOURCES)[number]; |
| 10 | + |
| 11 | +const SKILL_LIST_FLAGS = { |
| 12 | + file: { |
| 13 | + type: "string", |
| 14 | + valueHint: "<path>", |
| 15 | + description: "Config file path (default: agents.yaml)", |
| 16 | + }, |
| 17 | + source: { |
| 18 | + type: "string", |
| 19 | + valueHint: "<source>", |
| 20 | + description: |
| 21 | + "Skill catalog: custom (workspace-uploaded, default), official (built-in), or all (both catalogs in one call)", |
| 22 | + }, |
| 23 | + provider: { |
| 24 | + type: "string", |
| 25 | + valueHint: "<name>", |
| 26 | + description: "Target provider", |
| 27 | + }, |
| 28 | +} satisfies FlagsDef; |
| 29 | + |
| 30 | +export default defineCommand({ |
| 31 | + description: "List skills from the provider's skill catalog", |
| 32 | + auth: "apiKey", |
| 33 | + usageArgs: "[--source custom|official|all] [--provider <name>] [--file <path>]", |
| 34 | + flags: SKILL_LIST_FLAGS, |
| 35 | + exampleArgs: [ |
| 36 | + "", |
| 37 | + "--source official", |
| 38 | + "--source all --output json", |
| 39 | + "--source custom --provider bailian", |
| 40 | + ], |
| 41 | + notes: [ |
| 42 | + ...CREDENTIALS_NOTE, |
| 43 | + "Providers without a skill listing API (e.g. ark) return an empty list.", |
| 44 | + "For agent-driven skill selection, use `--source all --output json`: one call returns both catalogs with per-skill `source` and `description` fields to pick from.", |
| 45 | + ], |
| 46 | + validate: (f) => |
| 47 | + f.source && !SKILL_SOURCES.includes(f.source as SkillSource) |
| 48 | + ? "--source must be one of: custom, official, all." |
| 49 | + : undefined, |
| 50 | + async run(ctx) { |
| 51 | + const { settings, flags } = ctx; |
| 52 | + const format = detectOutputFormat(settings.output); |
| 53 | + const file = flags.file ?? "agents.yaml"; |
| 54 | + const source = (flags.source as SkillSource | undefined) ?? "custom"; |
| 55 | + |
| 56 | + const skills = await withAgentErrors(() => |
| 57 | + withStdoutProtected(async () => { |
| 58 | + const runtime = await buildAgentRuntime(ctx, file); |
| 59 | + if (source !== "all") { |
| 60 | + return listSkills(runtime, { provider: flags.provider, source }); |
| 61 | + } |
| 62 | + // Both catalogs in one call; each entry carries its own `source` field. |
| 63 | + const [customSkills, officialSkills] = await Promise.all([ |
| 64 | + listSkills(runtime, { provider: flags.provider, source: "custom" }), |
| 65 | + listSkills(runtime, { provider: flags.provider, source: "official" }), |
| 66 | + ]); |
| 67 | + return [...customSkills, ...officialSkills]; |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + if (format === "json") { |
| 72 | + emitResult({ source, skills }, format); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (skills.length === 0) { |
| 76 | + emitBare(source === "all" ? "No skills found." : `No ${source} skills found.`); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + const headers = ["ID", "NAME", "SOURCE", "STATUS", "VERSION", "CREATED"]; |
| 81 | + const rows = skills.map((skill) => [ |
| 82 | + skill.id, |
| 83 | + skill.name.slice(0, 32), |
| 84 | + skill.source, |
| 85 | + skill.status, |
| 86 | + skill.latest_version ?? "-", |
| 87 | + skill.created_at ?? "-", |
| 88 | + ]); |
| 89 | + for (const line of formatTable(headers, rows)) emitBare(line); |
| 90 | + emitBare(`\nTotal: ${skills.length} (${source})`); |
| 91 | + }, |
| 92 | +}); |
0 commit comments