@gtm-agents/cli
v0.3.0
Published
The gtm command: init, setup, doctor, migrate, brain, flags and deploy.
Readme
@gtm-agents/cli
The gtm command, for setting up and operating a deployment. This page is for deployers looking up a command's options and for contributors changing the CLI; docs/DEPLOYING.md walks through a first deployment in order.
Create a deployment from a published release with npx @gtm-agents/cli@<version> init <dir>; the new project lists @gtm-agents/cli as a dev dependency, so inside it the command is npx gtm. From a checkout of this repository, run npx gtm init <dir> --local <checkout> instead.
| Command | What it does |
|---|---|
| gtm init <dir> | Creates a new deployment from the worker template |
| gtm setup | Creates the Cloudflare resources, reusing any that exist, fills their ids into wrangler.jsonc, and lists the missing secrets |
| gtm migrate | Checks the migration order, then applies the D1 migrations |
| gtm brain init | A short interview that writes a draft brain and prints the matching gtm.config.ts lines |
| gtm brain validate \| upload \| diff | Validates the brain and templates, uploads them to KV with their locks, and shows what differs from KV |
| gtm flags list \| get \| set \| unset | Reads and writes feature flags in KV, checked against the flag registry |
| gtm doctor | Checks bindings, the cron trigger, configuration, adapters, secrets, webhooks, the kill switch and flags |
| gtm deploy | Runs doctor and the migrations, warns on a brain diff, then deploys |
Every command that talks to Cloudflare runs the deployment's installed Wrangler (npx --no-install wrangler ...) in the deployment's directory, with your Wrangler login, and takes --config <dir> (default: the current directory) and --env staging|production (default: production, the top level of wrangler.jsonc). Commands that write to production (migrate, brain upload, flags set|unset, deploy) ask first and take --yes to skip the question; without a terminal, --yes is required. Staging writes without asking. Wrangler itself runs without a terminal, so its own prompts auto-confirm. Exit codes: 0 success, 1 a check or a Wrangler call failed, 2 a usage error. The CLI runs on macOS and Linux; on Windows, use WSL.
Commands
gtm init <dir> [--name <worker-name>] [--local <checkout>] copies the worker template into a new, empty directory. It sets the Worker and bucket names, points migrations at the installed core package (node_modules/@gtm-agents/core/migrations), copies the built dashboard into public/admin (or a placeholder page when the dashboard has not been built), and writes package.json, tsconfig.json and .gitignore.
- By default (npm mode) the template is the copy bundled in the CLI package, and
package.jsondepends on@gtm-agents/core,@gtm-agents/adaptersand@gtm-agents/cliat exactly the CLI's own version, sonpm installfetches one matching release. - With
--local <checkout>, the template is the checkout'sapps/workerand the packages arefile:links into the checkout'spackages/. The path must be the checkout's root. - Both modes produce the same
wrangler.jsonc. Run from a checkout without--local, init uses the checkout's template, pins the checkout's version, and says that version may not be on npm.
gtm setup [--env] [--yes] [--reuse-existing] [--base-url <https-origin>] [--vector-dimensions <n>] [--vector-metric <metric>]
- Reads
wrangler.jsoncand plans a resource for every binding that still holds a${...}placeholder, plus the R2 bucket and any queue that does not exist yet: the D1 database, KV namespace, R2 bucket, Vectorize index and queues, named after the Worker and the binding (my-agent-db,my-agent-kv,my-agent-memory,my-agent-events,my-agent-events-dlq,my-agent-chat,my-agent-jobs,my-agent-jobs-dlq). - Looks each name up first (
d1 list,kv namespace list,r2 bucket info,vectorize list,queues info). If a lookup fails, it creates nothing. A resourcewrangler.jsoncalready names is reused; one that exists under a derived name the file does not hold yet is adopted only with--reuse-existing, since it may belong to another deployment with the same Worker name. An existing Vectorize index with other dimensions or metric is refused. - Prints the plan and asks before acting;
--yesskips the question, and without a terminal it is required. - Creates with
--update-config=false, so Wrangler never edits the file itself, then replaces only the placeholder strings inwrangler.jsonc, keeping comments and formatting. It does not write if the file changed while it ran. --base-urlfillsAGENT_BASE_URL. The Vectorize index defaults to 1536 dimensions and cosine distance, which cannot change after it is created.- A second run plans nothing. It ends with the chat queue's name and the jobs queue and its dead-letter queue, to list in
agent.chatQueue,scheduler.jobsQueueandscheduler.deadLetterQueueinsrc/index.ts, and with the missing secrets asnpx wrangler secret put <NAME>lines, fromwrangler secret list(names only).
gtm migrate [--env] [--local] checks that the migrations directory named by the DB binding exists and is numbered 0001, 0002, ... with no gaps or duplicates, then runs wrangler d1 migrations apply DB --remote (or --local). For the remote database it refuses while the database id is a placeholder.
gtm brain init [dir] [--answers <file.json>] and gtm brain init --questions (docs/BRAIN-SCHEMA.md)
- Asks seven batches of questions on the terminal: company and sender, buyers, accounts, pain, offer and proof, the booking link, the send window, and voice. A blank answer skips.
- Writes
personas,icp,message-pillars,factsandvoiceintodir(default./brain), every filestatus: draft. It never overwrites an existing component file, never fills a skipped answer, and never writesgtm.config.tsor the lock. The company, sender, booking and window answers are printed asgtm.config.tslines to paste. Then it runsgtm brain validateon the folder. - Every answer is checked as it is given, with the brain's own rules. On the terminal an invalid answer is asked again; an answers file with an invalid answer, an unknown question id or a value that is not text exits 2 and writes nothing.
--answersreads one JSON object of"<question id>": "<answer>"(at most 64 KB), for scripts and coding agents.--questionsprints the batches, each question's id, prompt, help and what it writes, as JSON.- Nothing is fetched and no model is called. To draft
product.mdandicp.mdfrom your website, use the dashboard's Product and ICP page, thengtm brain pull.
gtm brain validate [dir] [--templates <dir>] [--domain <domain>] [--write-lock] checks every brain component and template; with --write-lock it writes brain.lock.json when there are no errors (warnings, such as a missing component, are allowed).
gtm brain upload [dir] [--templates <dir>] [--domain <domain>] [--env] [--dry-run]
- Validates first, and uploads nothing on an error, when an approved component changed after it was locked, or when there is no lock.
- Computes the locks with the core functions and writes, through
wrangler kv bulk put --remote, every approved file (brain:file:<component>,template:file:<id>) and then, in a second call, the locks (brain:lock,templates:lock). The runtime serves a file only when it matches the lock, so a partial upload is never served. - The bulk files are written to a private temporary directory and deleted afterwards.
--dry-runlists the keys and sizes without calling Wrangler.
gtm brain diff [dir] [--templates <dir>] [--domain <domain>] [--env] reads brain:lock and templates:lock and lists each component and template that is new, changed or removed compared with the local files.
gtm flags list | get <key> | set <key> <value> | unset <key> [--env]
listshows every flag in the registry with its default and its remote value, including each member of a family, such asautonomy:<class>, that is set.setrefuses a key that is not in the registry and a value that does not fit its kind: booleans are stored astrueorfalse, numbers are whole and zero or more (except the sharesbudget:alert_threshold, above 0 and at most 1, andoutreach:holdout_rate, from 0 to 0.2), and a JSON flag takes a JSON object. A few string flags take only listed values (for examplebudget:enforcementissoftorhard). It refuses an autonomy level above 1 for a permanently human-only action class.- Reads go through
wrangler kv bulk get, writes throughwrangler kv bulk put, andunsetthroughwrangler kv key delete, all with--remote.
gtm doctor [--config <dir>] [--env] [--remote] reports PASS, WARN, FAIL or INFO for each check and exits non-zero on any FAIL. Offline it reads wrangler.jsonc (the required bindings, that the only cron trigger is * * * * *, that a JOBS_QUEUE has a consumer and a dead-letter queue, and the Durable Object migrations), gtm.config.ts, adapter manifests and the names in .dev.vars. --remote also reads the deployed secret names and the halt and kill-switch flags, and resolves the sending domain's SPF, DKIM (with sending.dkimSelector) and DMARC records over DNS over HTTPS; a lookup that cannot be made is a WARN, never a PASS or a FAIL.
gtm deploy [--env] [--dry-run] runs gtm doctor (any FAIL stops it), checks that no placeholder is left, runs gtm migrate, warns when gtm brain diff finds differences, then runs wrangler deploy. --dry-run lists the pending migrations and runs wrangler deploy --dry-run instead, changing nothing; while placeholders remain, it skips the migration and brain checks.
For contributors
- In this repository Node 24 runs the CLI's TypeScript directly; there is no build step. The published package is JavaScript, because Node does not strip types inside
node_modules: the release build compilessrc/todist/withtsconfig.build.jsonand bundles the worker template and the built dashboard intodist/template/, which init finds next to its own compiled file. Keep paths that init and the other commands resolve fromimport.meta.urlat the same depth insrc/anddist/. docs/RELEASING.md covers the build and the release smoke test. - Every Wrangler call goes through the injectable
CommandRunner(src/lib/io.ts). Tests replace it with an in-memory account (test/fake-wrangler.ts), so no test touches Cloudflare. src/lib/jsonc-edit.tseditswrangler.jsoncby replacing string values at their exact source positions. Never re-serialize a deployer's config.- No command reads or prints a secret value. Secrets are listed by name and set with
wrangler secret put, which prompts for the value.
