|
| 1 | +import { Command } from "commander"; |
| 2 | +import type { components } from "@shotstack/schemas"; |
| 3 | +import { createClient, type Client } from "../http/client.ts"; |
| 4 | +import { requireApiKey } from "../http/auth.ts"; |
| 5 | +import { resolveEnv, ENV_NAMES } from "../http/env.ts"; |
| 6 | +import { emit, parseOutputFormat } from "../output.ts"; |
| 7 | +import { withRecording, commandArgv } from "../recorder.ts"; |
| 8 | + |
| 9 | +export type GenerationModel = components["schemas"]["GenerationModel"]; |
| 10 | +type GenerationModelPricing = components["schemas"]["GenerationModelPricing"]; |
| 11 | +type GenerationModelList = components["schemas"]["GenerationModelListResponse"]; |
| 12 | + |
| 13 | +const UNIT_NOUNS: Record<string, string> = { clipSeconds: "second", promptCharacters: "character" }; |
| 14 | + |
| 15 | +export async function listModels(client: Client): Promise<GenerationModel[]> { |
| 16 | + const result = await client.get<GenerationModelList>("/models"); |
| 17 | + return result.models; |
| 18 | +} |
| 19 | + |
| 20 | +/** One model, including the JSON Schema for its `options`. */ |
| 21 | +export async function fetchModel(client: Client, id: string): Promise<GenerationModel> { |
| 22 | + return client.get<GenerationModel>(`/models/${encodeURIComponent(id)}`); |
| 23 | +} |
| 24 | + |
| 25 | +export const modelsCommand = new Command("models") |
| 26 | + .description("List generation models with availability and price, or show one model's options") |
| 27 | + .argument("[id]", "Model id; prints the JSON Schema for its options") |
| 28 | + .option(`--env <name>`, `Environment: ${ENV_NAMES.join(" | ")}`) |
| 29 | + .option("--output <format>", "Output format: text | json", "text") |
| 30 | + .action(async (id: string | undefined, options: { env?: string; output: string }) => { |
| 31 | + await withRecording("models", commandArgv("models"), async () => { |
| 32 | + const format = parseOutputFormat(options.output); |
| 33 | + const env = resolveEnv(options.env); |
| 34 | + const apiKey = requireApiKey(env.name); |
| 35 | + const client = createClient({ apiKey, env }); |
| 36 | + |
| 37 | + if (id) { |
| 38 | + const model = await fetchModel(client, id); |
| 39 | + emit(format, model, formatModelDetail(model)); |
| 40 | + return { response: { model: model.model, available: model.available } }; |
| 41 | + } |
| 42 | + |
| 43 | + const models = await listModels(client); |
| 44 | + emit(format, models, models.length ? formatModelTable(models) : "No models."); |
| 45 | + return { response: { count: models.length } }; |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | +// `available` is omitted when the account could not be read; that is not a yes. |
| 50 | +export function formatAvailability(model: GenerationModel): string { |
| 51 | + if (model.available === true) return "available"; |
| 52 | + if (model.available === false) return `unavailable: ${model.unavailableReason ?? "no reason given"}`; |
| 53 | + return "availability unknown"; |
| 54 | +} |
| 55 | + |
| 56 | +export function formatPrice(pricing: GenerationModelPricing): string { |
| 57 | + const unit = formatUnit(pricing.quantity); |
| 58 | + if (typeof pricing.credits === "number") return `${pricing.credits} ${pricing.credits === 1 ? "credit" : "credits"} ${unit}`; |
| 59 | + const fallback = pricing.tieredBy?.default; |
| 60 | + const tiers = Object.entries(pricing.credits) |
| 61 | + .map(([tier, credits]) => `${tier} ${credits}${tier === fallback ? " (default)" : ""}`) |
| 62 | + .join(", "); |
| 63 | + return `credits ${unit} by ${pricing.tieredBy?.option ?? "option"}: ${tiers}`; |
| 64 | +} |
| 65 | + |
| 66 | +function formatUnit(quantity: GenerationModelPricing["quantity"]): string { |
| 67 | + if (!quantity) return "per generation"; |
| 68 | + const noun = UNIT_NOUNS[quantity.measure] ?? quantity.measure; |
| 69 | + return quantity.per === 1 ? `per ${noun}` : `per ${quantity.per} ${noun}s`; |
| 70 | +} |
| 71 | + |
| 72 | +function formatModelTable(models: GenerationModel[]): string { |
| 73 | + const rows = models.map((m) => [m.model, m.type, formatAvailability(m), m.pricing ? formatPrice(m.pricing) : ""]); |
| 74 | + const widths = rows[0]!.map((_, col) => Math.max(...rows.map((row) => row[col]!.length))); |
| 75 | + return rows.map((row) => row.map((cell, col) => cell.padEnd(widths[col]!)).join(" ").trimEnd()).join("\n"); |
| 76 | +} |
| 77 | + |
| 78 | +function formatModelDetail(model: GenerationModel): string { |
| 79 | + const lines = [`${model.model} ${model.type} ${formatAvailability(model)}`]; |
| 80 | + if (model.name) lines.push(model.description ? `${model.name}: ${model.description}` : model.name); |
| 81 | + if (model.pricing) lines.push(`Price: ${formatPrice(model.pricing)}`); |
| 82 | + if (model.options) lines.push("Options:", JSON.stringify(model.options, null, 2)); |
| 83 | + return lines.join("\n"); |
| 84 | +} |
0 commit comments