|
| 1 | +import { defineCommand, detectOutputFormat, modelsPermissionsPath } from "bailian-cli-core"; |
| 2 | +import { emitResult, renderBoxTable } from "bailian-cli-runtime"; |
| 3 | +import { buildQuery } from "../shared/params.ts"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Types — mirror GET /api/v1/models/permissions |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +interface PermissionDetail { |
| 10 | + inference?: boolean | null; |
| 11 | + fine_tune?: boolean | null; |
| 12 | + deploy?: boolean | null; |
| 13 | +} |
| 14 | + |
| 15 | +interface ModelPermission { |
| 16 | + model: string; |
| 17 | + name?: string; |
| 18 | + permissions?: PermissionDetail; |
| 19 | +} |
| 20 | + |
| 21 | +interface PermissionsResponse { |
| 22 | + output?: { |
| 23 | + total?: number; |
| 24 | + page_no?: number; |
| 25 | + page_size?: number; |
| 26 | + permissions?: ModelPermission[]; |
| 27 | + }; |
| 28 | + request_id?: string; |
| 29 | +} |
| 30 | + |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | +// Formatters |
| 33 | +// --------------------------------------------------------------------------- |
| 34 | + |
| 35 | +/** Tri-state permission cell: true → yes, false → no, null/undefined → "-". */ |
| 36 | +function formatGrant(granted: boolean | null | undefined): string { |
| 37 | + if (granted == null) return "-"; |
| 38 | + return granted ? "yes" : "no"; |
| 39 | +} |
| 40 | + |
| 41 | +function printTable(permissions: ModelPermission[], total: number, emptyHint: string): void { |
| 42 | + if (permissions.length === 0) { |
| 43 | + process.stdout.write(`No model permissions found.\n${emptyHint}\n`); |
| 44 | + return; |
| 45 | + } |
| 46 | + const headers = ["Model", "Name", "Inference", "Fine-tune", "Deploy"]; |
| 47 | + const rows = permissions.map((entry) => [ |
| 48 | + entry.model, |
| 49 | + entry.name ?? "-", |
| 50 | + formatGrant(entry.permissions?.inference), |
| 51 | + formatGrant(entry.permissions?.fine_tune), |
| 52 | + formatGrant(entry.permissions?.deploy), |
| 53 | + ]); |
| 54 | + const lines = renderBoxTable({ |
| 55 | + headers, |
| 56 | + rows, |
| 57 | + align: ["left", "left", "right", "right", "right"], |
| 58 | + }); |
| 59 | + for (const line of lines) process.stdout.write(line + "\n"); |
| 60 | + process.stdout.write(`\nTotal: ${total}\n`); |
| 61 | +} |
| 62 | + |
| 63 | +// --------------------------------------------------------------------------- |
| 64 | +// Command |
| 65 | +// --------------------------------------------------------------------------- |
| 66 | + |
| 67 | +export default defineCommand({ |
| 68 | + description: "List model permissions (inference / fine-tune / deploy) in the workspace", |
| 69 | + auth: "apiKey", |
| 70 | + usageArgs: "[--scope <scope>] [--model <model>] [--name <name>] [--page <n>] [--page-size <n>]", |
| 71 | + flags: { |
| 72 | + scope: { |
| 73 | + type: "string", |
| 74 | + valueHint: "<scope>", |
| 75 | + choices: ["authorized", "authorizable"] as const, |
| 76 | + description: "Authorization scope: authorizable (default, full catalog), authorized", |
| 77 | + }, |
| 78 | + model: { |
| 79 | + type: "string", |
| 80 | + valueHint: "<model>", |
| 81 | + description: "Model ID (exact match)", |
| 82 | + }, |
| 83 | + name: { |
| 84 | + type: "string", |
| 85 | + valueHint: "<name>", |
| 86 | + description: "Fuzzy search by model name or ID", |
| 87 | + }, |
| 88 | + page: { type: "number", valueHint: "<n>", description: "Page number (default: 1)" }, |
| 89 | + pageSize: { type: "number", valueHint: "<n>", description: "Results per page (default: 20)" }, |
| 90 | + }, |
| 91 | + exampleArgs: [ |
| 92 | + "", |
| 93 | + "--model qwen-plus", |
| 94 | + "--scope authorized", |
| 95 | + "--name qwen --page-size 50", |
| 96 | + "--output text", |
| 97 | + ], |
| 98 | + notes: [ |
| 99 | + "Default scope is `authorizable` (the full grantable catalog); use `--scope authorized` to see only models already granted.", |
| 100 | + "Output defaults to JSON; pass `--output text` for a table. Permission values are tri-state: true / false / null (never set).", |
| 101 | + "Values mirror the server's grant records as-is for the workspace bound to your API key. A model reporting false/null can still be callable (access may come from other channels); see the Model Studio authorization docs for the exact semantics.", |
| 102 | + ], |
| 103 | + async run(ctx) { |
| 104 | + const { settings, flags } = ctx; |
| 105 | + const format = settings.outputExplicit ? detectOutputFormat(settings.output) : "json"; |
| 106 | + const scope = flags.scope ?? "authorizable"; |
| 107 | + |
| 108 | + const query = { |
| 109 | + authorization_scope: scope.toUpperCase(), |
| 110 | + model: flags.model || undefined, |
| 111 | + name: flags.name || undefined, |
| 112 | + page_no: flags.page || 1, |
| 113 | + page_size: flags.pageSize || 20, |
| 114 | + }; |
| 115 | + |
| 116 | + if (settings.dryRun) { |
| 117 | + emitResult( |
| 118 | + { endpoint: ctx.client.url(modelsPermissionsPath()), method: "GET", query }, |
| 119 | + format, |
| 120 | + ); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const resp = await ctx.client.requestJson<PermissionsResponse>({ |
| 125 | + path: modelsPermissionsPath() + buildQuery(query), |
| 126 | + }); |
| 127 | + const permissions = resp.output?.permissions ?? []; |
| 128 | + const total = resp.output?.total ?? permissions.length; |
| 129 | + |
| 130 | + if (format === "json") { |
| 131 | + emitResult({ items: permissions, total }, format); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // The default authorized view is empty until something is granted — point |
| 136 | + // at the authorizable catalog instead of ending with a bare "nothing". |
| 137 | + const binName = ctx.identity.binName; |
| 138 | + const emptyHint = |
| 139 | + scope === "authorized" |
| 140 | + ? `Nothing granted yet in this workspace. Browse grantable models with \`${binName} permission list --scope authorizable\`, then grant with \`${binName} permission grant --model <model>\`.` |
| 141 | + : `Adjust --name/--model filters, or check pagination with --page/--page-size.`; |
| 142 | + |
| 143 | + printTable(permissions, total, emptyHint); |
| 144 | + }, |
| 145 | +}); |
0 commit comments