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