|
| 1 | +import { |
| 2 | + BailianError, |
| 3 | + ExitCode, |
| 4 | + defineCommand, |
| 5 | + detectInstalledAgents, |
| 6 | + fetchSkillsIndex, |
| 7 | + getSkillRegistryBaseUrl, |
| 8 | + installSkillWithFanout, |
| 9 | + readSkillLock, |
| 10 | + runWithConcurrency, |
| 11 | + writeSkillLock, |
| 12 | +} from "bailian-cli-core"; |
| 13 | +import { emitBare, emitResult, formatTable } from "bailian-cli-runtime"; |
| 14 | + |
| 15 | +interface InitOutcome { |
| 16 | + name: string; |
| 17 | + status: "installed" | "failed"; |
| 18 | + publishedAt?: string; |
| 19 | + agents?: string[]; |
| 20 | + reason?: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** Prefix used to identify first-party Bailian skills in the registry. */ |
| 24 | +const BAILIAN_PREFIX = "bailian-"; |
| 25 | + |
| 26 | +/** Max number of skills downloading/installing at the same time. */ |
| 27 | +const INIT_CONCURRENCY = 3; |
| 28 | + |
| 29 | +export default defineCommand({ |
| 30 | + description: "Install all bailian-* skills (one-shot bootstrap for new environments)", |
| 31 | + auth: "none", |
| 32 | + usageArgs: "", |
| 33 | + exampleArgs: [""], |
| 34 | + notes: [ |
| 35 | + "Fetches the registry index and installs every skill whose name starts with bailian-", |
| 36 | + "Equivalent to: bl skill add --all (filtered to bailian-* skills)", |
| 37 | + ], |
| 38 | + async run(ctx) { |
| 39 | + const format = ctx.settings.outputExplicit ? ctx.settings.output : "json"; |
| 40 | + const index = await fetchSkillsIndex(); |
| 41 | + |
| 42 | + // Discover all bailian-* skills from the live registry index |
| 43 | + const names = Object.keys(index.skills).filter((name) => name.startsWith(BAILIAN_PREFIX)); |
| 44 | + |
| 45 | + const lock = readSkillLock(); |
| 46 | + const agents = detectInstalledAgents(); |
| 47 | + |
| 48 | + const tasks = names.map((name) => async (): Promise<InitOutcome> => { |
| 49 | + const entry = index.skills[name]; |
| 50 | + try { |
| 51 | + const record = await installSkillWithFanout( |
| 52 | + name, |
| 53 | + entry, |
| 54 | + agents, |
| 55 | + lock.skills[name]?.links ?? [], |
| 56 | + ); |
| 57 | + lock.skills[name] = record.lockEntry; |
| 58 | + return { |
| 59 | + name, |
| 60 | + status: "installed", |
| 61 | + publishedAt: entry.publishedAt, |
| 62 | + agents: record.linkedAgents, |
| 63 | + }; |
| 64 | + } catch (err) { |
| 65 | + return { |
| 66 | + name, |
| 67 | + status: "failed", |
| 68 | + reason: err instanceof Error ? err.message : String(err), |
| 69 | + }; |
| 70 | + } |
| 71 | + }); |
| 72 | + const results = await runWithConcurrency(tasks, INIT_CONCURRENCY); |
| 73 | + writeSkillLock(lock); |
| 74 | + |
| 75 | + if (format === "json") { |
| 76 | + emitResult( |
| 77 | + { |
| 78 | + registry: getSkillRegistryBaseUrl(), |
| 79 | + agents: agents.map((agent) => agent.id), |
| 80 | + skills: results, |
| 81 | + }, |
| 82 | + format, |
| 83 | + ); |
| 84 | + } else if (results.length === 0) { |
| 85 | + emitBare("No bailian-* skills found in the registry."); |
| 86 | + } else { |
| 87 | + const rows = results.map((result) => [ |
| 88 | + result.name, |
| 89 | + result.status, |
| 90 | + result.publishedAt ? result.publishedAt.slice(0, 10) : "-", |
| 91 | + result.status === "installed" ? result.agents?.join(", ") || "-" : (result.reason ?? "-"), |
| 92 | + ]); |
| 93 | + for (const line of formatTable(["NAME", "STATUS", "PUBLISHED", "AGENTS / REASON"], rows)) { |
| 94 | + emitBare(line); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const failed = results.filter((result) => result.status === "failed"); |
| 99 | + if (failed.length > 0) { |
| 100 | + throw new BailianError( |
| 101 | + `${failed.length}/${results.length} skill(s) failed to install`, |
| 102 | + ExitCode.GENERAL, |
| 103 | + "Check the reason for failed skills in the output; network failures can be retried with bl skill init", |
| 104 | + ); |
| 105 | + } |
| 106 | + }, |
| 107 | +}); |
0 commit comments