@domesystems/templates 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/package.json +1 -1
- package/src/cli.mjs +31 -11
- package/src/generate.mjs +208 -0
- package/src/registry.mjs +1 -0
- package/src/template-files.mjs +19 -0
package/README.md
CHANGED
|
@@ -5,10 +5,11 @@ Pull immutable Dome template releases, publish public templates from GitHub, and
|
|
|
5
5
|
```bash
|
|
6
6
|
npx @domesystems/templates login --registry https://templates-registry.domesystems.ai --github
|
|
7
7
|
npx @domesystems/templates pull dome-systems/ai-agent-for-documentation --registry https://templates-registry.domesystems.ai --dir ai-agent-for-documentation
|
|
8
|
+
npx @domesystems/templates generate --dir ai-agent-for-documentation
|
|
8
9
|
npx @domesystems/templates up --dir ai-agent-for-documentation
|
|
9
10
|
```
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
Remote commands use `https://templates-registry.domesystems.ai` by default. Pass either `--server <Dome application URL>` to discover a different Registry endpoint through an app server, or `--registry <Registry URL>` to connect directly. A successful login makes that endpoint the default for later remote commands; either flag overrides it. Credentials are isolated per selected endpoint.
|
|
12
13
|
|
|
13
14
|
Use direct Registry endpoints when the app-server discovery document is unavailable:
|
|
14
15
|
|
|
@@ -28,11 +29,12 @@ templates login [--server <URL>|--registry <URL>] [--github]
|
|
|
28
29
|
templates logout [--server <URL>|--registry <URL>]
|
|
29
30
|
templates doctor <pull|up|publish> [--dir <directory>] [--server <URL>|--registry <URL>] [--json]
|
|
30
31
|
templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]
|
|
31
|
-
templates
|
|
32
|
+
templates generate [--dir <directory>] [--output <file>] [--force]
|
|
33
|
+
templates up [--dir <directory>] [--port <host-port>] [--server <URL>|--registry <URL>] [--import-job <job-id>] [--skip-provision|--provision] [--no-start] [--detach] [--verbose]
|
|
32
34
|
templates publish [--dry-run] [--server <URL>|--registry <URL>]
|
|
33
35
|
```
|
|
34
36
|
|
|
35
|
-
`pull` only accepts scoped package names, verifies the Registry SHA-256 before extraction, accepts only the archive's `template/` root, writes `.dome-template-lock.yaml`, and never overwrites a non-empty directory. `up` is local-only and never downloads or updates a release. `publish` reads the scoped name and SemVer version from `template.yaml`, requires a clean Git checkout, and cannot replace a published version.
|
|
37
|
+
`pull` only accepts scoped package names, verifies the Registry SHA-256 before extraction, accepts only the archive's `template/` root, writes `.dome-template-lock.yaml`, and never overwrites a non-empty directory. `generate` reads `catalog` in `template.yaml` and writes a `dome.tf` with the matching Gateway, connections, model pool, agent, Rules, Guards, quotas, and runtime key. It never replaces an existing file unless you pass `--force`; review the generated Terraform before importing it. `up` is local-only and never downloads or updates a release. When it provisions an unconfigured runtime, it reveals the import's one-time agent token, resolves the workspace default Gateway, and writes only `DOME_TOKEN` and `DOME_GATEWAY_URL` to the ignored `.env` before starting Docker. `publish` reads the scoped name and SemVer version from `template.yaml`, requires a clean Git checkout, and cannot replace a published version.
|
|
36
38
|
|
|
37
39
|
Use `--verbose` with a command to include its diagnostic stack trace when it fails. When publishing requires a GitHub connection, the CLI opens the authorization page automatically; complete it, then run `publish` again.
|
|
38
40
|
|
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { spawn, spawnSync } from "node:child_process";
|
|
4
|
-
import readline from "node:readline/promises";
|
|
5
4
|
import { readManifest, readPublishManifest } from "./manifest.mjs";
|
|
5
|
+
import { generateDomeTerraform } from "./generate.mjs";
|
|
6
6
|
import { imageForRuntime, renderCompose } from "./runtime.mjs";
|
|
7
|
-
import { extractRelease, initializeEnvironment } from "./template-files.mjs";
|
|
7
|
+
import { extractRelease, initializeEnvironment, writeRuntimeEnvironment } from "./template-files.mjs";
|
|
8
8
|
import { createArchive, publishSource } from "./publish-files.mjs";
|
|
9
|
-
import {
|
|
9
|
+
import { DEFAULT_REGISTRY, clearSelectedTarget, deleteProfile, directRegistry, discover, login, normalizeServer, readProfile, readSelectedTarget, registryRequest, writeSelectedTarget } from "./registry.mjs";
|
|
10
10
|
|
|
11
11
|
function ansi(code, value, options) { return options.color && process.stdout.isTTY ? `\u001B[${code}m${value}\u001B[0m` : value; }
|
|
12
12
|
function line(kind, value, options) { const styles = { info: ["•", "36"], success: ["✓", "32"], warning: ["!", "33"], error: ["✗", "31"] }; const [mark, code] = styles[kind]; return `${ansi(code, mark, options)} ${value}`; }
|
|
13
|
-
function usage() { return "Usage:\n templates login [--server <Dome application URL>|--registry <Registry URL>] [--github]\n templates logout [--server <Dome application URL>|--registry <Registry URL>]\n templates doctor <pull|up|publish> [package] [--dir <directory>] [--server <URL>|--registry <URL>] [--json]\n templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]\n templates up [--dir <directory>] [--port <host-port>] [--skip-provision|--provision] [--no-start] [--detach] [--verbose]\n templates publish [--dry-run] [--server <Dome application URL>|--registry <Registry URL>]\n"; }
|
|
13
|
+
function usage() { return "Usage:\n templates login [--server <Dome application URL>|--registry <Registry URL>] [--github]\n templates logout [--server <Dome application URL>|--registry <Registry URL>]\n templates doctor <pull|up|publish> [package] [--dir <directory>] [--server <URL>|--registry <URL>] [--json]\n templates pull <scope/package[@version]> [--dir <directory>] [--server <URL>|--registry <URL>]\n templates generate [--dir <directory>] [--output <file>] [--force]\n templates up [--dir <directory>] [--port <host-port>] [--server <URL>|--registry <URL>] [--import-job <job-id>] [--skip-provision|--provision] [--no-start] [--detach] [--verbose]\n templates publish [--dry-run] [--server <Dome application URL>|--registry <Registry URL>]\n"; }
|
|
14
14
|
function parse(argv) {
|
|
15
15
|
if (!argv.length || argv.includes("--help") || argv.includes("-h")) return { help: true };
|
|
16
16
|
const [command, ...rest] = argv;
|
|
17
|
-
const options = { server: undefined, directory: undefined, port: 3000, dryRun: false, json: false, github: false, provision: true, forceProvision: false, start: true, detach: false, verbose: false, color: !process.env.NO_COLOR };
|
|
17
|
+
const options = { server: undefined, directory: undefined, output: "dome.tf", force: false, port: 3000, dryRun: false, json: false, github: false, importJob: undefined, provision: true, forceProvision: false, start: true, detach: false, verbose: false, color: !process.env.NO_COLOR };
|
|
18
18
|
const values = [];
|
|
19
19
|
for (let i = 0; i < rest.length; i += 1) {
|
|
20
20
|
const arg = rest[i];
|
|
21
|
-
if (arg === "--server" || arg === "--registry" || arg === "--dir" || arg === "--port" || arg === "--color") { const value = rest[++i]; if (!value) throw new Error(`${arg} requires a value.`); if (arg === "--server") options.server = normalizeServer(value); else if (arg === "--registry") options.registry = normalizeServer(value); else if (arg === "--dir") options.directory = value; else if (arg === "--color") { if (!["auto", "always", "never"].includes(value)) throw new Error("--color must be auto, always, or never."); options.color = value === "always" || (value === "auto" && !process.env.NO_COLOR); } else { options.port = Number(value); if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error("--port requires an integer from 1 to 65535."); } }
|
|
22
|
-
else if (arg === "--github") options.github = true; else if (arg === "--dry-run") options.dryRun = true; else if (arg === "--json") options.json = true; else if (arg === "--skip-provision") options.provision = false; else if (arg === "--provision") options.forceProvision = true; else if (arg === "--no-start") options.start = false; else if (arg === "--detach") options.detach = true; else if (arg === "--verbose") options.verbose = true; else if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}`); else values.push(arg);
|
|
21
|
+
if (arg === "--server" || arg === "--registry" || arg === "--dir" || arg === "--output" || arg === "--port" || arg === "--color" || arg === "--import-job") { const value = rest[++i]; if (!value) throw new Error(`${arg} requires a value.`); if (arg === "--server") options.server = normalizeServer(value); else if (arg === "--registry") options.registry = normalizeServer(value); else if (arg === "--dir") options.directory = value; else if (arg === "--output") options.output = value; else if (arg === "--import-job") options.importJob = value; else if (arg === "--color") { if (!["auto", "always", "never"].includes(value)) throw new Error("--color must be auto, always, or never."); options.color = value === "always" || (value === "auto" && !process.env.NO_COLOR); } else { options.port = Number(value); if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error("--port requires an integer from 1 to 65535."); } }
|
|
22
|
+
else if (arg === "--github") options.github = true; else if (arg === "--dry-run") options.dryRun = true; else if (arg === "--json") options.json = true; else if (arg === "--force") options.force = true; else if (arg === "--skip-provision") options.provision = false; else if (arg === "--provision") options.forceProvision = true; else if (arg === "--no-start") options.start = false; else if (arg === "--detach") options.detach = true; else if (arg === "--verbose") options.verbose = true; else if (arg.startsWith("-")) throw new Error(`Unknown option: ${arg}`); else values.push(arg);
|
|
23
23
|
}
|
|
24
24
|
if (options.server && options.registry) throw new Error("Use either --server or --registry, not both.");
|
|
25
|
-
if (!["login", "logout", "doctor", "pull", "up", "publish"].includes(command)) throw new Error(`Unknown command: ${command}`);
|
|
25
|
+
if (!["login", "logout", "doctor", "pull", "generate", "up", "publish"].includes(command)) throw new Error(`Unknown command: ${command}`);
|
|
26
26
|
return { command, values, options };
|
|
27
27
|
}
|
|
28
|
-
function remoteTarget(options) { return options.registry ? directRegistry(options.registry) : options.server ?? readSelectedTarget() ??
|
|
28
|
+
function remoteTarget(options) { return options.registry ? directRegistry(options.registry) : options.server ?? readSelectedTarget() ?? directRegistry(DEFAULT_REGISTRY); }
|
|
29
29
|
function remoteLocation(options) { const target = remoteTarget(options); return typeof target === "string" ? `--server ${target}` : `--registry ${target.registry}`; }
|
|
30
30
|
const githubPublishingAccessMessage = "GitHub publishing access is required.";
|
|
31
31
|
function openBrowser(url) {
|
|
@@ -53,9 +53,29 @@ async function pull(name, options, output, fetchImplementation) {
|
|
|
53
53
|
fs.mkdirSync(target, { recursive: true }); await extractRelease(Buffer.from(await response.arrayBuffer()), target, release); const created = initializeEnvironment(target);
|
|
54
54
|
output(line("success", `Pulled ${release.name}@${release.version}`, options)); output(` Saved to ${target}`); if (created) output(line("success", "Created .env from .env.example", options));
|
|
55
55
|
}
|
|
56
|
+
function generate(options, output) {
|
|
57
|
+
const target = path.resolve(process.cwd(), options.directory ?? ".");
|
|
58
|
+
const manifest = readManifest(target);
|
|
59
|
+
const file = generateDomeTerraform(target, { output: options.output, force: options.force, manifest });
|
|
60
|
+
output(line("success", `Generated ${file}`, options));
|
|
61
|
+
}
|
|
56
62
|
function run(command, args, cwd) { const result = spawnSync(command, args, { cwd, stdio: "inherit" }); if (result.error || result.status !== 0) throw new Error(`${command} ${args.join(" ")} failed.`); }
|
|
63
|
+
function json(text, command) { try { return JSON.parse(text.slice(text.indexOf("{"))); } catch { throw new Error(`${command} did not return valid JSON.`); } }
|
|
64
|
+
function runJSON(command, args, cwd, { reveal = false } = {}) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const child = spawn(command, args, { cwd, stdio: ["inherit", "pipe", "inherit"] });
|
|
67
|
+
let stdout = "";
|
|
68
|
+
child.stdout.on("data", (chunk) => { stdout += chunk; if (reveal) process.stdout.write(chunk); });
|
|
69
|
+
child.on("error", () => reject(new Error(`${command} ${args.join(" ")} failed.`)));
|
|
70
|
+
child.on("close", (status) => status === 0 ? resolve(json(stdout, command)) : reject(new Error(`${command} ${args.join(" ")} failed.`)));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
57
73
|
function configured(target) { const env = path.join(target, ".env"); return fs.existsSync(env) && ["DOME_TOKEN", "DOME_GATEWAY_URL"].every((key) => new RegExp(`^${key}=(?!\\s*$|undefined|\"\")`, "m").test(fs.readFileSync(env, "utf8"))); }
|
|
58
|
-
|
|
74
|
+
export function importMadeNoChanges(job) { const summary = job?.plan_summary ?? job?.planSummary; if (!summary) return false; return ["create_count", "update_count", "import_count"].every((key) => Number(summary[key] ?? summary[key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())] ?? 0) === 0); }
|
|
75
|
+
export function importHasPendingOutputs(job) { return (job?.outputs_state ?? job?.outputsState) === "pending"; }
|
|
76
|
+
export function importCreatedResources(job) { const summary = job?.plan_summary ?? job?.planSummary; return Number(summary?.create_count ?? summary?.createCount ?? 0) > 0; }
|
|
77
|
+
async function configureRuntime(target, jobID, options, output) { const gateways = await runJSON("dome", ["gateways", "list", "--json"], target); const gatewayURL = gateways?.data?.gateways?.find((candidate) => candidate.is_default)?.endpoints?.gateway_url; if (!gatewayURL) throw new Error("The import did not configure a default Gateway for the runtime."); const revealed = await runJSON("dome", ["import", "outputs", jobID, "--json"], target); const tokens = Object.entries(revealed?.data?.outputs ?? {}).filter(([key, value]) => key.endsWith("_token") && typeof value === "string"); if (tokens.length !== 1) throw new Error("The import did not produce exactly one runtime agent token."); writeRuntimeEnvironment(target, { DOME_TOKEN: tokens[0][1], DOME_GATEWAY_URL: gatewayURL }); output(line("success", "Configured the runtime with the imported agent credentials.", options)); }
|
|
78
|
+
async function up(options, output) { const target = path.resolve(process.cwd(), options.directory ?? "."); const manifest = readManifest(target); const needsCredentials = !configured(target); output(line("info", `Checking ${target}`, options)); fs.writeFileSync(path.join(target, ".dome-compose.yaml"), renderCompose(manifest.app.runtime, options.port)); if (options.importJob) { output(line("info", "Saving credentials from the selected Dome import", options)); await configureRuntime(target, options.importJob, options, output); } else if (options.provision && (needsCredentials || options.forceProvision)) { output(line("info", "Setting up Dome resources", options)); const imported = await runJSON("dome", ["import", "dome.tf", "--json"], target); const job = imported?.data?.job; if (!job?.id) throw new Error("Dome import did not return a job ID."); if (importCreatedResources(job) && importHasPendingOutputs(job)) await configureRuntime(target, job.id, options, output); else if (needsCredentials) throw new Error(`Dome matched existing resources and did not create a new runtime token. Recover a pending import with templates up --dir ${options.directory ?? "."} --import-job <job-id>, or rotate the runtime key and set DOME_TOKEN and DOME_GATEWAY_URL in .env.`); else output(line("success", "Dome resources are already up to date; keeping the existing runtime credentials.", options)); } if (options.start) { output(line("info", `Starting app at http://localhost:${options.port}`, options)); run("docker", ["compose", "-f", ".dome-compose.yaml", "up", ...(options.detach ? ["--detach"] : [])], target); } }
|
|
59
79
|
async function publish(options, output, fetchImplementation) { const target = path.resolve(process.cwd(), options.directory ?? "."); const manifest = readPublishManifest(target); const source = publishSource(target); const name = manifest.metadata.name; const [scope, slug] = name.split("/"); const archive = options.dryRun ? null : createArchive(source); output(line("info", `Checking ${name}@${manifest.metadata.version}`, options)); const payload = { name, version: manifest.metadata.version, title: manifest.metadata.title, summary: manifest.metadata.summary ?? "", sha256: archive?.sha256, size_bytes: archive?.buffer.length, source: { repository: manifest.metadata.source.repository, path: manifest.metadata.source.path, commit: source.commit, remote: source.remote, files: source.files } };
|
|
60
80
|
const remote = remoteTarget(options); let registry; let attempt;
|
|
61
81
|
try { ({ target: registry, body: attempt } = await registryRequest(remote, "/v1/publish-attempts", { method: "POST", body: { ...payload, dry_run: options.dryRun }, authenticated: true, fetchImplementation })); } catch (caught) {
|
|
@@ -68,4 +88,4 @@ async function publish(options, output, fetchImplementation) { const target = pa
|
|
|
68
88
|
async function doctor(subject, options, output, fetchImplementation) { const checks = []; const add = (name, passed, remediation) => checks.push({ name, passed, remediation }); const remote = remoteTarget(options); try { if (subject === "pull") { if (typeof remote === "string") await discover(remote, fetchImplementation); add("Registry reachability", true); } else if (subject === "up") { const target = path.resolve(process.cwd(), options.directory ?? "."); readManifest(target); add("template.yaml", true); add("Docker Compose", spawnSync("docker", ["compose", "version"], { stdio: "ignore" }).status === 0, "Install Docker Desktop or Docker Engine with the Compose plugin."); add("Dome CLI", spawnSync("dome", ["--version"], { stdio: "ignore" }).status === 0, "Install the Dome CLI."); } else if (subject === "publish") { readPublishManifest(path.resolve(process.cwd(), options.directory ?? ".")); publishSource(path.resolve(process.cwd(), options.directory ?? ".")); const profile = readProfile(remote.server ?? remote); add("Registry sign-in", Boolean(profile?.token), `Run npx @domesystems/templates login ${remoteLocation(options)}`); } else throw new Error("doctor expects pull, up, or publish."); } catch (error) { add("Preflight", false, error.message); }
|
|
69
89
|
if (options.json) output(JSON.stringify({ server: remote.registry ?? remote, checks })); else checks.forEach((check) => output(line(check.passed ? "success" : "error", check.passed ? check.name : `${check.name}: ${check.remediation}`, options))); if (checks.some((check) => !check.passed)) throw new Error(`Cannot continue until ${subject} checks pass.`);
|
|
70
90
|
}
|
|
71
|
-
export async function main(argv, { output = console.log, error = console.error, fetchImplementation = globalThis.fetch, sleep, exit = (code) => { process.exitCode = code; }, open = openBrowser } = {}) { let options; try { const parsed = parse(argv); if (parsed.help) return output(usage()); const { command, values } = parsed; options = parsed.options; const remote = remoteTarget(options); if (options.json) options.color = false; if (command === "login") { const destination = options.registry ?? options.server ?? (typeof remote === "string" ? remote : remote.registry); output(line("info", `Opening sign-in for ${destination}`, options)); const session = await login(remote, { github: options.github, fetchImplementation, sleep, onStart: (started) => { open(started.verification_uri); output(options.verbose ? `Opening ${started.verification_uri}.` : `If your browser didn't open, open ${started.verification_uri} and enter ${started.user_code ?? "the displayed code"}.`); } }); writeSelectedTarget(remote); output(line("success", `Signed in as ${session.session.email}`, options)); return; } if (command === "logout") { const server = remote.server ?? remote; const profile = readProfile(server); if (profile?.token) await registryRequest(remote, "/v1/auth/logout", { method: "POST", authenticated: true, fetchImplementation }); deleteProfile(server); clearSelectedTarget(remote); output(line("success", `Signed out of ${server}`, options)); return; } if (command === "pull") return await pull(values[0], options, output, fetchImplementation); if (command === "up") return await up(options, output); if (command === "publish") return await publish(options, output, fetchImplementation); return await doctor(values[0], options, output, fetchImplementation); } catch (caught) { const detail = options?.verbose && caught?.stack ? caught.stack : caught.message; error(line("info", `Error: ${detail}`, { color: false })); exit(1); } }
|
|
91
|
+
export async function main(argv, { output = console.log, error = console.error, fetchImplementation = globalThis.fetch, sleep, exit = (code) => { process.exitCode = code; }, open = openBrowser } = {}) { let options; try { const parsed = parse(argv); if (parsed.help) return output(usage()); const { command, values } = parsed; options = parsed.options; const remote = remoteTarget(options); if (options.json) options.color = false; if (command === "login") { const destination = options.registry ?? options.server ?? (typeof remote === "string" ? remote : remote.registry); output(line("info", `Opening sign-in for ${destination}`, options)); const session = await login(remote, { github: options.github, fetchImplementation, sleep, onStart: (started) => { open(started.verification_uri); output(options.verbose ? `Opening ${started.verification_uri}.` : `If your browser didn't open, open ${started.verification_uri} and enter ${started.user_code ?? "the displayed code"}.`); } }); writeSelectedTarget(remote); output(line("success", `Signed in as ${session.session.email}`, options)); return; } if (command === "logout") { const server = remote.server ?? remote; const profile = readProfile(server); if (profile?.token) await registryRequest(remote, "/v1/auth/logout", { method: "POST", authenticated: true, fetchImplementation }); deleteProfile(server); clearSelectedTarget(remote); output(line("success", `Signed out of ${server}`, options)); return; } if (command === "pull") return await pull(values[0], options, output, fetchImplementation); if (command === "generate") return generate(options, output); if (command === "up") return await up(options, output); if (command === "publish") return await publish(options, output, fetchImplementation); return await doctor(values[0], options, output, fetchImplementation); } catch (caught) { const detail = options?.verbose && caught?.stack ? caught.stack : caught.message; error(line("info", `Error: ${detail}`, { color: false })); exit(1); } }
|
package/src/generate.mjs
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const label = (value) => {
|
|
5
|
+
const rendered = String(value ?? "").replace(/[^A-Za-z0-9_]/g, "_").replace(/^[^A-Za-z_]/, "_");
|
|
6
|
+
if (!rendered) throw new Error("Template resources need a name.");
|
|
7
|
+
return rendered;
|
|
8
|
+
};
|
|
9
|
+
const q = (value) => JSON.stringify(String(value));
|
|
10
|
+
const attr = (name, value) => `${name.padEnd(19)} = ${value}`;
|
|
11
|
+
const block = (type, name, body) => `resource ${q(type)} ${q(name)} {\n${body.map((line) => ` ${line}`).join("\n")}\n}`;
|
|
12
|
+
const variable = (name) => `variable ${q(name)} {\n type = string\n sensitive = true\n}`;
|
|
13
|
+
const nameFor = (prefix, value) => `${prefix}_${label(value)}`;
|
|
14
|
+
|
|
15
|
+
function required(object, key, message) {
|
|
16
|
+
if (!object?.[key]) throw new Error(message);
|
|
17
|
+
return object[key];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function secretVariable(secret) {
|
|
21
|
+
return secret?.variable ? label(secret.variable) : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function connectionBlocks(connection, gatewayLabel, variables) {
|
|
25
|
+
const connectionLabel = label(required(connection, "name", "Every catalog connection needs a name."));
|
|
26
|
+
const auth = connection.auth ?? { method: "none" };
|
|
27
|
+
const lines = [
|
|
28
|
+
attr("workspace_id", "var.workspace_id"),
|
|
29
|
+
attr("name", q(connection.name)),
|
|
30
|
+
attr("protocol", q(required(connection, "protocol", `Connection ${connection.name} needs a protocol.`))),
|
|
31
|
+
attr("url", q(required(connection, "url", `Connection ${connection.name} needs a URL.`))),
|
|
32
|
+
attr("auth_method", q(auth.method)),
|
|
33
|
+
];
|
|
34
|
+
if (auth.credential) lines.push(attr("credential_type", q(auth.credential)));
|
|
35
|
+
if (auth.oauth) {
|
|
36
|
+
const oauth = Object.entries(auth.oauth).map(([key, value]) => {
|
|
37
|
+
const destination = key === "scopes" ? "default_scopes" : key;
|
|
38
|
+
return `${destination} = ${Array.isArray(value) ? `[${value.map(q).join(", ")}]` : q(value)}`;
|
|
39
|
+
});
|
|
40
|
+
lines.push(`oauth = { ${oauth.join(", ")} }`);
|
|
41
|
+
}
|
|
42
|
+
const secret = auth.secret;
|
|
43
|
+
if (secretVariable(secret)) {
|
|
44
|
+
const key = auth.header ?? "Authorization";
|
|
45
|
+
variables.add(secretVariable(secret));
|
|
46
|
+
lines.push(attr("secret_values", `{ ${q(key)} = var.${secretVariable(secret)} }`));
|
|
47
|
+
}
|
|
48
|
+
const output = [block("dome_mcp_connection", connectionLabel, lines)];
|
|
49
|
+
for (const tool of [...(connection.permit ?? []), ...(connection.forbid ?? [])]) {
|
|
50
|
+
output.push(block("dome_gateway_tool", nameFor(connectionLabel, tool), [
|
|
51
|
+
attr("workspace_id", "var.workspace_id"),
|
|
52
|
+
attr("gateway", `dome_gateway.${gatewayLabel}.name`),
|
|
53
|
+
attr("mcp_connection", `dome_mcp_connection.${connectionLabel}.name`),
|
|
54
|
+
attr("tool", q(tool)),
|
|
55
|
+
]));
|
|
56
|
+
}
|
|
57
|
+
return output;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function modelBlocks(model, pool, gatewayLabel, variables) {
|
|
61
|
+
const modelLabel = label(required(model, "name", "catalog.model needs a name."));
|
|
62
|
+
const poolLabel = label(required(pool, "name", "catalog.pool needs a name."));
|
|
63
|
+
const secret = model.secret;
|
|
64
|
+
const modelLines = [
|
|
65
|
+
attr("workspace_id", "var.workspace_id"),
|
|
66
|
+
attr("name", q(model.name)),
|
|
67
|
+
attr("provider_id", q(required(model, "provider", "catalog.model needs a provider."))),
|
|
68
|
+
attr("provider_config", `jsonencode({ model = ${q(required(model, "model_id", "catalog.model needs a model_id."))} })`),
|
|
69
|
+
];
|
|
70
|
+
if (secretVariable(secret)) {
|
|
71
|
+
const variableName = secretVariable(secret);
|
|
72
|
+
variables.add(variableName);
|
|
73
|
+
modelLines.push(attr("auth_method", q("api-key")), attr("credential_type", q("shared")), attr("secret_values", `{ ${q(model.secret_header ?? "Authorization")} = var.${variableName} }`));
|
|
74
|
+
}
|
|
75
|
+
return [
|
|
76
|
+
block("dome_llm_connection", modelLabel, modelLines),
|
|
77
|
+
block("dome_llm_pool", poolLabel, [
|
|
78
|
+
attr("workspace_id", "var.workspace_id"),
|
|
79
|
+
attr("name", q(pool.name)),
|
|
80
|
+
attr("is_default", String(Boolean(pool.default))),
|
|
81
|
+
]),
|
|
82
|
+
block("dome_llm_pool_member", modelLabel, [
|
|
83
|
+
attr("workspace_id", "var.workspace_id"),
|
|
84
|
+
attr("pool", `dome_llm_pool.${poolLabel}.name`),
|
|
85
|
+
attr("llm_connection", `dome_llm_connection.${modelLabel}.name`),
|
|
86
|
+
]),
|
|
87
|
+
block("dome_gateway_llm_pool", poolLabel, [
|
|
88
|
+
attr("workspace_id", "var.workspace_id"),
|
|
89
|
+
attr("gateway", `dome_gateway.${gatewayLabel}.name`),
|
|
90
|
+
attr("llm_pool", `dome_llm_pool.${poolLabel}.name`),
|
|
91
|
+
]),
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function rulesBlock(root, rules, agentLabel) {
|
|
96
|
+
if (!rules?.file) return [];
|
|
97
|
+
const file = path.resolve(root, rules.file);
|
|
98
|
+
if (!fs.statSync(file, { throwIfNoEntry: false })?.isFile()) throw new Error(`Rules file not found: ${rules.file}.`);
|
|
99
|
+
const filename = path.basename(rules.file);
|
|
100
|
+
const contents = fs.readFileSync(file, "utf8").trimEnd();
|
|
101
|
+
return [block("dome_agent_rules_bundle", agentLabel, [
|
|
102
|
+
attr("workspace_id", "var.workspace_id"),
|
|
103
|
+
attr("agent", `dome_agent.${agentLabel}.name`),
|
|
104
|
+
"files = {",
|
|
105
|
+
` ${q(filename)} = <<-CEDAR`,
|
|
106
|
+
...contents.split("\n").map((line) => ` ${line}`),
|
|
107
|
+
" CEDAR",
|
|
108
|
+
"}",
|
|
109
|
+
])];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function guardBlocks(root, guards, refs) {
|
|
113
|
+
const filters = guards?.filters ?? [];
|
|
114
|
+
const blocks = [];
|
|
115
|
+
const bindings = new Map();
|
|
116
|
+
for (const filter of filters) {
|
|
117
|
+
const filterLabel = label(required(filter, "name", "Every Guard filter needs a name."));
|
|
118
|
+
let config;
|
|
119
|
+
if (filter.redact_ssn) config = "jsonencode({ text = { components = [{ action = \"FILTER_ACTION_REDACT\", matchers = [{ ssn = {} }] }] } })";
|
|
120
|
+
else if (filter.config) {
|
|
121
|
+
const file = path.resolve(root, filter.config);
|
|
122
|
+
if (!fs.statSync(file, { throwIfNoEntry: false })?.isFile()) throw new Error(`Guard config not found: ${filter.config}.`);
|
|
123
|
+
config = `jsonencode(jsondecode(<<-JSON\n${fs.readFileSync(file, "utf8").trimEnd()}\nJSON\n))`;
|
|
124
|
+
} else throw new Error(`Guard ${filter.name} needs config or redact_ssn.`);
|
|
125
|
+
blocks.push(block("dome_filter", filterLabel, [attr("workspace_id", "var.workspace_id"), attr("name", q(filter.name)), attr("description", q(filter.description ?? filter.name)), attr("config", config)]));
|
|
126
|
+
const binding = required(filter, "bind", `Guard ${filter.name} needs a bind target.`);
|
|
127
|
+
const kind = binding.model ? "model" : binding.tool ? "tool" : null;
|
|
128
|
+
if (!kind) throw new Error(`Guard ${filter.name} must bind to a model or tool.`);
|
|
129
|
+
const target = kind === "model" ? label(binding.model) : label(binding.tool);
|
|
130
|
+
const key = `${kind}:${target}:${binding.direction ?? "request"}`;
|
|
131
|
+
bindings.set(key, [...(bindings.get(key) ?? []), filterLabel]);
|
|
132
|
+
}
|
|
133
|
+
for (const [key, filterLabels] of bindings) {
|
|
134
|
+
const [kind, target, direction] = key.split(":");
|
|
135
|
+
const resource = kind === "model" ? "dome_llm_connection_filters" : "dome_mcp_connection_filters";
|
|
136
|
+
const attribute = kind === "model" ? "llm_connection" : "mcp_connection";
|
|
137
|
+
blocks.push(block(resource, `${target}_${direction}`, [
|
|
138
|
+
attr("workspace_id", "var.workspace_id"),
|
|
139
|
+
attr(attribute, `${kind === "model" ? "dome_llm_connection" : "dome_mcp_connection"}.${target}.name`),
|
|
140
|
+
attr("direction", q(direction)),
|
|
141
|
+
attr("filters", `[${filterLabels.map((item) => `dome_filter.${item}.name`).join(", ")}]`),
|
|
142
|
+
]));
|
|
143
|
+
}
|
|
144
|
+
return blocks;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function quotaBlocks(quotas, refs) {
|
|
148
|
+
return (quotas ?? []).map((quota) => {
|
|
149
|
+
const subject = required(quota, "subject", "Every quota needs a subject.");
|
|
150
|
+
const target = subject === "agent" ? quota.agent : subject === "model" ? quota.model : quota.connection ?? String(quota.tool ?? "").split("/")[0];
|
|
151
|
+
if (!target) throw new Error(`Quota ${quota.name ?? ""} needs a target.`);
|
|
152
|
+
const resource = subject === "agent" ? "dome_agent" : subject === "model" ? "dome_llm_connection" : "dome_mcp_connection";
|
|
153
|
+
const type = subject === "agent" ? "agent" : subject === "model" ? "model" : "tool_connection";
|
|
154
|
+
const amount = quota.unit === "dome_usd" ? Math.round(Number(quota.limit) * 1_000_000) : Number(quota.limit);
|
|
155
|
+
if (!Number.isFinite(amount)) throw new Error(`Quota ${quota.name ?? ""} needs a numeric limit.`);
|
|
156
|
+
return block("dome_quota", label(quota.name ?? `${subject}_${target}`), [
|
|
157
|
+
attr("workspace_id", "var.workspace_id"),
|
|
158
|
+
attr("dimension", q(type === "tool_connection" ? "tool" : "llm")),
|
|
159
|
+
attr("unit", q(required(quota, "unit", "Every quota needs a unit."))),
|
|
160
|
+
attr("subject_type", q(type)),
|
|
161
|
+
attr("subject", `${resource}.${label(target)}.name`),
|
|
162
|
+
attr("scope", q(quota.per_caller ? "per_caller" : "total")),
|
|
163
|
+
attr("window", q(required(quota, "window", "Every quota needs a window."))),
|
|
164
|
+
attr("limit_amount", String(amount)),
|
|
165
|
+
attr("name", q(required(quota, "name", "Every quota needs a name."))),
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function renderDomeTerraform(manifest, root) {
|
|
171
|
+
const catalog = required(manifest, "catalog", "template.yaml must declare catalog to generate dome.tf.");
|
|
172
|
+
const gateway = required(catalog, "gateway", "catalog.gateway is required to generate dome.tf.");
|
|
173
|
+
const agent = required(catalog, "agent", "catalog.agent is required to generate dome.tf.");
|
|
174
|
+
const gatewayLabel = label(required(gateway, "name", "catalog.gateway needs a name."));
|
|
175
|
+
const agentLabel = label(required(agent, "name", "catalog.agent needs a name."));
|
|
176
|
+
const variables = new Set();
|
|
177
|
+
const blocks = [
|
|
178
|
+
block("dome_gateway", gatewayLabel, [
|
|
179
|
+
attr("workspace_id", "var.workspace_id"), attr("name", q(gateway.name)),
|
|
180
|
+
...(gateway.description ? [attr("description", q(gateway.description))] : []), attr("is_default", String(Boolean(gateway.default))),
|
|
181
|
+
]),
|
|
182
|
+
...(catalog.connections ?? []).flatMap((connection) => connectionBlocks(connection, gatewayLabel, variables)),
|
|
183
|
+
];
|
|
184
|
+
if (catalog.model || catalog.pool) {
|
|
185
|
+
if (!catalog.model || !catalog.pool) throw new Error("catalog.model and catalog.pool must be declared together.");
|
|
186
|
+
blocks.push(...modelBlocks(catalog.model, catalog.pool, gatewayLabel, variables));
|
|
187
|
+
}
|
|
188
|
+
const actAs = agent.act_as;
|
|
189
|
+
const agentLines = [attr("workspace_id", "var.workspace_id"), attr("name", q(agent.name)), attr("allowed_gateways", `[dome_gateway.${gatewayLabel}.name]`)];
|
|
190
|
+
if (actAs?.method && actAs.method !== "none") {
|
|
191
|
+
agentLines.push(attr("act_as", `{ method = ${q(actAs.method)}, required = ${String(Boolean(actAs.required))} }`));
|
|
192
|
+
if (actAs.secret) { const variableName = label(actAs.secret); variables.add(variableName); agentLines.push(attr("act_as_hmac_secret", `var.${variableName}`)); }
|
|
193
|
+
if (actAs.allowed_subjects) agentLines.push(attr("actas_allowed_subjects", `[${actAs.allowed_subjects.map(q).join(", ")}]`));
|
|
194
|
+
}
|
|
195
|
+
blocks.push(block("dome_agent", agentLabel, agentLines), ...rulesBlock(root, catalog.rules, agentLabel), ...guardBlocks(root, catalog.guards, {}), ...quotaBlocks(catalog.quotas, {}));
|
|
196
|
+
blocks.push(block("dome_agent_key", "runtime", [attr("workspace_id", "var.workspace_id"), attr("agent", `dome_agent.${agentLabel}.name`), attr("name", q("runtime"))]));
|
|
197
|
+
const header = `terraform {\n required_providers {\n dome = { source = ${q("dome-systems/dome")}, version = ${q(">= 0.1.0")} }\n }\n}\n\nvariable "workspace_id" { type = string }`;
|
|
198
|
+
const secretDeclarations = [...variables].sort().map(variable).join("\n\n");
|
|
199
|
+
const output = `output ${q(`${agentLabel}_token`)} {\n value = dome_agent_key.runtime.token\n sensitive = true\n}`;
|
|
200
|
+
return [header, secretDeclarations, blocks.join("\n\n"), output].filter(Boolean).join("\n\n") + "\n";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function generateDomeTerraform(directory, { output = "dome.tf", force = false, manifest }) {
|
|
204
|
+
const target = path.resolve(directory, output);
|
|
205
|
+
if (fs.existsSync(target) && !force) throw new Error(`${target} already exists. Use --force to replace it.`);
|
|
206
|
+
fs.writeFileSync(target, renderDomeTerraform(manifest, directory));
|
|
207
|
+
return target;
|
|
208
|
+
}
|
package/src/registry.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import os from "node:os";
|
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
|
|
6
6
|
export const DEFAULT_SERVER = "https://app.domesystems.ai";
|
|
7
|
+
export const DEFAULT_REGISTRY = "https://templates-registry.domesystems.ai";
|
|
7
8
|
|
|
8
9
|
function profileKey(server) {
|
|
9
10
|
return crypto.createHash("sha256").update(new URL(server).origin).digest("hex").slice(0, 24);
|
package/src/template-files.mjs
CHANGED
|
@@ -44,6 +44,25 @@ export function initializeEnvironment(templateDirectory) {
|
|
|
44
44
|
return false;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
export function writeRuntimeEnvironment(templateDirectory, values) {
|
|
48
|
+
const environment = path.join(templateDirectory, ".env");
|
|
49
|
+
const required = ["DOME_TOKEN", "DOME_GATEWAY_URL"];
|
|
50
|
+
for (const key of required) {
|
|
51
|
+
if (typeof values[key] !== "string" || !values[key].trim() || /[\r\n]/.test(values[key])) {
|
|
52
|
+
throw new Error(`Cannot configure runtime: ${key} is missing or invalid.`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let text = fs.existsSync(environment) ? fs.readFileSync(environment, "utf8") : "";
|
|
57
|
+
for (const key of required) {
|
|
58
|
+
const assignment = `${key}=${values[key]}`;
|
|
59
|
+
const pattern = new RegExp(`^${key}=.*$`, "m");
|
|
60
|
+
text = pattern.test(text) ? text.replace(pattern, assignment) : `${text}${text && !text.endsWith("\n") ? "\n" : ""}${assignment}\n`;
|
|
61
|
+
}
|
|
62
|
+
fs.writeFileSync(environment, text, { mode: 0o600 });
|
|
63
|
+
fs.chmodSync(environment, 0o600);
|
|
64
|
+
}
|
|
65
|
+
|
|
47
66
|
export function writeLock(destination, release) {
|
|
48
67
|
fs.writeFileSync(path.join(destination, ".dome-template-lock.yaml"), stringify({
|
|
49
68
|
apiVersion: "templates.domesystems.ai/v1", package: release.name, version: release.version, sha256: release.sha256,
|