|
| 1 | +import type { Argv } from "yargs" |
| 2 | +import { cmd } from "./cmd" |
| 3 | +import * as prompts from "@clack/prompts" |
| 4 | +import fs from "fs/promises" |
| 5 | +import path from "path" |
| 6 | +import os from "os" |
| 7 | + |
| 8 | +const BASE_URL = "https://apimi.tryaltimate.com" |
| 9 | + |
| 10 | +function getAltimateDotDir(): string { |
| 11 | + return path.join(os.homedir(), ".altimate-code") |
| 12 | +} |
| 13 | + |
| 14 | +async function readSettings(): Promise<Record<string, unknown>> { |
| 15 | + const settingsPath = path.join(getAltimateDotDir(), "settings.json") |
| 16 | + try { |
| 17 | + return JSON.parse(await fs.readFile(settingsPath, "utf-8")) |
| 18 | + } catch { |
| 19 | + return {} |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +async function writeSettings(settings: Record<string, unknown>): Promise<void> { |
| 24 | + const dir = getAltimateDotDir() |
| 25 | + await fs.mkdir(dir, { recursive: true }) |
| 26 | + await fs.writeFile(path.join(dir, "settings.json"), JSON.stringify(settings, null, 2)) |
| 27 | +} |
| 28 | + |
| 29 | +// Injected at build time by build.ts (same pattern as ALTIMATE_CLI_MIGRATIONS). |
| 30 | +// In development these fall back to reading from disk via getAssets(). |
| 31 | +declare const ALTIMATE_VALIDATE_SKILL_MD: string |
| 32 | +declare const ALTIMATE_VALIDATE_BATCH_PY: string |
| 33 | + |
| 34 | +interface ValidateAssets { |
| 35 | + skillMd: string |
| 36 | + batchPy: string |
| 37 | +} |
| 38 | + |
| 39 | +async function getAssets(): Promise<ValidateAssets> { |
| 40 | + if ( |
| 41 | + typeof ALTIMATE_VALIDATE_SKILL_MD !== "undefined" && |
| 42 | + typeof ALTIMATE_VALIDATE_BATCH_PY !== "undefined" |
| 43 | + ) { |
| 44 | + return { |
| 45 | + skillMd: ALTIMATE_VALIDATE_SKILL_MD, |
| 46 | + batchPy: ALTIMATE_VALIDATE_BATCH_PY, |
| 47 | + } |
| 48 | + } |
| 49 | + // Development fallback: read from disk relative to this source file |
| 50 | + const skillsDir = path.join(import.meta.dir, "../../skill/validate") |
| 51 | + const [skillMd, batchPy] = await Promise.all([ |
| 52 | + fs.readFile(path.join(skillsDir, "SKILL.md"), "utf-8"), |
| 53 | + fs.readFile(path.join(skillsDir, "batch_validate.py"), "utf-8"), |
| 54 | + ]) |
| 55 | + return { skillMd, batchPy } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +const InstallSubcommand = cmd({ |
| 61 | + command: "install", |
| 62 | + describe: "install the /validate skill into ~/.altimate-code", |
| 63 | + handler: async () => { |
| 64 | + prompts.intro("Altimate Validate — Installer") |
| 65 | + |
| 66 | + const { skillMd, batchPy } = await getAssets() |
| 67 | + |
| 68 | + const spinner = prompts.spinner() |
| 69 | + spinner.start("Installing /validate skill...") |
| 70 | + const skillTargetDir = path.join(os.homedir(), ".altimate-code", "skills", "validate") |
| 71 | + await fs.mkdir(skillTargetDir, { recursive: true }) |
| 72 | + await fs.writeFile(path.join(skillTargetDir, "SKILL.md"), skillMd) |
| 73 | + await fs.writeFile(path.join(skillTargetDir, "batch_validate.py"), batchPy) |
| 74 | + spinner.stop(`Installed /validate skill → ${skillTargetDir}`) |
| 75 | + |
| 76 | + prompts.outro("Altimate validation skill installed successfully!") |
| 77 | + }, |
| 78 | +}) |
| 79 | + |
| 80 | +const StatusSubcommand = cmd({ |
| 81 | + command: "status", |
| 82 | + describe: "check whether the /validate skill is installed", |
| 83 | + handler: async () => { |
| 84 | + const skillDir = path.join(os.homedir(), ".altimate-code", "skills", "validate") |
| 85 | + |
| 86 | + prompts.intro("Altimate Validate — Installation Status") |
| 87 | + |
| 88 | + const check = (exists: boolean, label: string, detail: string) => |
| 89 | + prompts.log.info(`${exists ? "✓" : "✗"} ${label}${exists ? "" : " (not found)"}: ${detail}`) |
| 90 | + |
| 91 | + const skillMdExists = await fs.access(path.join(skillDir, "SKILL.md")).then(() => true).catch(() => false) |
| 92 | + const batchPyExists = await fs.access(path.join(skillDir, "batch_validate.py")).then(() => true).catch(() => false) |
| 93 | + check(skillMdExists && batchPyExists, "/validate skill", skillDir) |
| 94 | + |
| 95 | + prompts.outro("Done") |
| 96 | + }, |
| 97 | +}) |
| 98 | + |
| 99 | +const ConfigureSubcommand = cmd({ |
| 100 | + command: "configure", |
| 101 | + describe: "register your Altimate API key to enable /validate", |
| 102 | + builder: (yargs: Argv) => |
| 103 | + yargs.option("api-key", { type: "string", description: "Your Altimate API key" }), |
| 104 | + handler: async (args) => { |
| 105 | + prompts.intro("Altimate Validate — Configure") |
| 106 | + |
| 107 | + const apiKey = |
| 108 | + (args["api-key"] as string | undefined) || |
| 109 | + ((await prompts.text({ |
| 110 | + message: "Enter your Altimate API key:", |
| 111 | + placeholder: "8a5b279d...", |
| 112 | + validate: (v) => ((v ?? "").trim().length > 0 ? undefined : "API key is required"), |
| 113 | + })) as string) |
| 114 | + |
| 115 | + if (prompts.isCancel(apiKey)) { |
| 116 | + prompts.cancel("Cancelled.") |
| 117 | + process.exit(0) |
| 118 | + } |
| 119 | + |
| 120 | + const spinner = prompts.spinner() |
| 121 | + spinner.start("Registering with validation server...") |
| 122 | + |
| 123 | + try { |
| 124 | + const res = await fetch(`${BASE_URL}/auth/register`, { |
| 125 | + method: "POST", |
| 126 | + headers: { "Content-Type": "application/json" }, |
| 127 | + body: JSON.stringify({ api_key: apiKey }), |
| 128 | + }) |
| 129 | + |
| 130 | + if (!res.ok) { |
| 131 | + const body = await res.text() |
| 132 | + spinner.stop("Registration failed.") |
| 133 | + prompts.log.error(`Server returned ${res.status}: ${body}`) |
| 134 | + process.exit(1) |
| 135 | + } |
| 136 | + |
| 137 | + spinner.stop("Registered with validation server.") |
| 138 | + } catch (err) { |
| 139 | + spinner.stop("Could not reach validation server.") |
| 140 | + prompts.log.warn(`Warning: ${err}. Credentials saved locally anyway.`) |
| 141 | + } |
| 142 | + |
| 143 | + // Save credentials to ~/.altimate-code/settings.json |
| 144 | + const settings = await readSettings() |
| 145 | + settings.altimate_api_key = apiKey |
| 146 | + await writeSettings(settings) |
| 147 | + |
| 148 | + prompts.log.success(`Credentials saved to ${path.join(getAltimateDotDir(), "settings.json")}`) |
| 149 | + prompts.outro("Configuration complete. You can now run /validate.") |
| 150 | + }, |
| 151 | +}) |
| 152 | + |
| 153 | +export const ValidateCommand = cmd({ |
| 154 | + command: "validate", |
| 155 | + describe: "manage the Altimate validation framework (/validate skill)", |
| 156 | + builder: (yargs: Argv) => |
| 157 | + yargs |
| 158 | + .command(InstallSubcommand) |
| 159 | + .command(StatusSubcommand) |
| 160 | + .command(ConfigureSubcommand) |
| 161 | + .demandCommand(), |
| 162 | + handler: () => {}, |
| 163 | +}) |
0 commit comments