import chalk from "chalk"; import Table from "cli-table3"; import { loadConfig, getApiBase, getApiKey } from "../auth/config"; export async function modelsCommand(): Promise { const apiKey = getApiKey(); if (!apiKey) { console.log(chalk.red('Not authenticated. Run "llmtune login" first.')); process.exit(1); } const apiBase = getApiBase(); try { const modelsUrl = apiBase.replace(/\/$/, "") + "/models"; const response = await fetch(modelsUrl, { headers: { Authorization: `Bearer ${apiKey}`, }, }); if (!response.ok) { const body = await response.text(); console.log(chalk.red(`Failed to fetch models: ${response.status}`)); console.log(chalk.dim(body.slice(0, 200))); process.exit(1); } const data = (await response.json()) as { data: Array<{ id: string; object: string; created: number; owned_by: string; }>; subscription?: { planName: string; planModels: string[]; quotaDaily: number; }; }; if (data.subscription) { console.log( chalk.cyan(`\nSubscription: ${data.subscription.planName}`) ); console.log( chalk.dim( `Daily quota: ${data.subscription.quotaDaily} requests\n` ) ); } const table = new Table({ head: [ chalk.cyan("Model ID"), chalk.cyan("Provider"), ], colWidths: [50, 20], }); for (const model of data.data) { table.push([model.id, model.owned_by]); } console.log(table.toString()); console.log(chalk.dim(`\n${data.data.length} models available`)); } catch (err) { console.log( chalk.red(`Error: ${err instanceof Error ? err.message : String(err)}`) ); process.exit(1); } }