@answerlayer/cli 0.1.0 → 0.1.2

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 CHANGED
@@ -55,19 +55,25 @@ not change how `answerlayer` works from your terminal.
55
55
 
56
56
  ## Configure
57
57
 
58
- Create an API key in AnswerLayer with the scopes needed by your workflow, then run:
58
+ Create an API key in AnswerLayer with the scopes needed by your workflow, then
59
+ initialize the CLI. `init` verifies the key before it writes the config file, so
60
+ an invalid or expired key never overwrites a working local setup:
59
61
 
60
62
  ```bash
61
- answerlayer configure --api-key al_live_...
63
+ answerlayer init --api-key al_live_...
62
64
  ```
63
65
 
64
66
  The CLI talks to the hosted SaaS at `https://app.answerlayer.io` by default. For a
65
67
  BYOC or self-hosted install, point it at your own URL:
66
68
 
67
69
  ```bash
68
- answerlayer configure --api-key al_live_... --base-url https://answerlayer.your-company.com
70
+ answerlayer init --api-key al_live_... --base-url https://answerlayer.your-company.com
69
71
  ```
70
72
 
73
+ The first API key is currently created in the AnswerLayer app under **Settings →
74
+ API Keys**. A browser/device login flow is planned; until then, the CLI will tell
75
+ you exactly what credential is missing rather than saving an unverified value.
76
+
71
77
  You can also skip the config file and use environment variables:
72
78
 
73
79
  ```bash
@@ -77,6 +83,35 @@ export ANSWERLAYER_BASE_URL=https://answerlayer.your-company.com # optional ove
77
83
 
78
84
  The CLI sends API keys using the `X-API-Key` header.
79
85
 
86
+ ## Run locally with an agent
87
+
88
+ The package includes an `answerlayer` agent skill for evaluating AnswerLayer
89
+ against your own database without first creating a hosted account. Install it
90
+ for Codex with:
91
+
92
+ ```bash
93
+ answerlayer skills install
94
+ ```
95
+
96
+ This installs to `~/.codex/skills/answerlayer` by default. Use `--path` for a
97
+ different skill directory; an existing skill is never replaced unless you pass
98
+ `--force`.
99
+
100
+ The flow is:
101
+
102
+ 1. Install the CLI/plugin.
103
+ 2. Clone and start the local `answerlayer-core` Docker stack.
104
+ 3. Run `make local-bootstrap` in that stack to create a local-only organization
105
+ and scoped CLI key.
106
+ 4. Configure the CLI for `http://localhost:8000` with the printed command.
107
+ 5. Connect a dedicated, read-only database user and test a small, approved
108
+ schema first.
109
+
110
+ The local stack never needs a hosted AnswerLayer account. The skill requires
111
+ agent confirmation before it starts containers, creates a connection, or sends
112
+ queries to a real database, and it instructs agents not to print or persist
113
+ database passwords.
114
+
80
115
  Useful scopes:
81
116
 
82
117
  - `api_key:manage` for API key management
@@ -147,6 +182,16 @@ npm test
147
182
 
148
183
  The package intentionally has no runtime dependencies. It requires Node.js 20 or newer for built-in `fetch`.
149
184
 
185
+ ## Releasing
186
+
187
+ Releases are automated with [semantic-release](https://semantic-release.gitbook.io/).
188
+ Every push to `main` derives the next version from commit messages and publishes
189
+ to npm via OIDC — no manual version bump, tag, or token:
190
+
191
+ - `fix:` / `feat:` / `chore:` / unprefixed … → patch (`0.0.X`)
192
+ - `feat(minor): …` → minor (`0.X.0`)
193
+ - `type!: …` or `BREAKING CHANGE:` in the body → major (`X.0.0`)
194
+
150
195
  ## Related projects
151
196
 
152
197
  - [`answerlayer-go`](https://github.com/AnswerLayer/answerlayer-go) — Go client/SDK for the Inquiry API.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@answerlayer/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Open-source command line client for the AnswerLayer API.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,6 +10,7 @@
10
10
  "files": [
11
11
  "bin",
12
12
  "src",
13
+ "skills",
13
14
  "README.md",
14
15
  "LICENSE"
15
16
  ],
@@ -0,0 +1,177 @@
1
+ ---
2
+ name: answerlayer
3
+ description: Query databases in natural language with AnswerLayer. Use when the user wants to set up or configure AnswerLayer, connect to their data, ask analytical or business questions about a database (revenue, users, counts, "top N", trends, metrics), run SQL through AnswerLayer, or manage AnswerLayer connections, saved queries, the semantic layer, or dashboards.
4
+ ---
5
+
6
+ # AnswerLayer
7
+
8
+ AnswerLayer turns natural-language questions into governed SQL against the user's
9
+ connected databases. This skill drives the **AnswerLayer CLI**, which talks to
10
+ the AnswerLayer API with an API key.
11
+
12
+ Work in three steps: **resolve the CLI → ensure it's configured → use it.**
13
+
14
+ When a user asks to evaluate AnswerLayer locally or against a real database
15
+ without creating a hosted account, use the local-stack workflow below instead
16
+ of the hosted-key workflow.
17
+
18
+ ## Local stack workflow
19
+
20
+ Use this only when the user explicitly wants a local evaluation or authorizes
21
+ you to start Docker containers and create a local API key. Explain the commands
22
+ that will run and get confirmation before each state-changing phase.
23
+
24
+ Install this bundled skill with `answerlayer skills install` when it is not
25
+ already available in the Codex skills directory.
26
+
27
+ 1. Install the CLI if it is not already available:
28
+
29
+ ```bash
30
+ npm install -g @answerlayer/cli
31
+ ```
32
+
33
+ 2. Clone the core stack beside the user's work and prepare its environment:
34
+
35
+ ```bash
36
+ git clone https://github.com/AnswerLayer/answerlayer-core.git
37
+ cd answerlayer-core
38
+ cp .env.example .env
39
+ ```
40
+
41
+ Ask the user to supply a real `ANTHROPIC_API_KEY` and a strong local
42
+ `ENCRYPTION_KEY` in `.env`. Do not print, echo, or add either value to a
43
+ command transcript. Clerk and Stripe placeholders are not needed for the
44
+ local API-key workflow.
45
+
46
+ 3. After confirmation, start the local stack and wait for its health endpoint:
47
+
48
+ ```bash
49
+ docker compose up --build -d
50
+ curl --fail http://localhost:8000/healthz
51
+ ```
52
+
53
+ 4. After confirmation, create the local-only organization and CLI key:
54
+
55
+ ```bash
56
+ make local-bootstrap
57
+ ```
58
+
59
+ The command rotates any previous `local-cli-bootstrap` key and prints an
60
+ `answerlayer init --base-url http://localhost:8000 --api-key ...` command.
61
+ Have the user run that printed command in their terminal. Never copy the key
62
+ into chat, source control, logs, or a shell command you display back to them.
63
+
64
+ 5. Before connecting real data, require a dedicated database account that is
65
+ restricted to approved schemas and `SELECT` only. Show the proposed
66
+ connection host, database, and username, then get confirmation before
67
+ running `connections create` or any query. Put the password only in a local,
68
+ permission-restricted JSON file; never print it or commit it.
69
+
70
+ 6. Start with a small approved schema and read-only inspection. Confirm before
71
+ creating connections, generating semantic objects, or executing queries.
72
+
73
+ This workflow is for Docker Compose development only. Do not run
74
+ `local-bootstrap` against a hosted or customer deployment.
75
+
76
+ ## 1. Resolve the CLI command (`AL`)
77
+
78
+ Pick the command once per session and reuse it. Run:
79
+
80
+ ```bash
81
+ command -v answerlayer.js >/dev/null && echo answerlayer.js \
82
+ || command -v answerlayer >/dev/null && echo answerlayer \
83
+ || echo "npx -y @answerlayer/cli"
84
+ ```
85
+
86
+ - `answerlayer.js` — ships with this plugin and is on your PATH (works offline).
87
+ - `answerlayer` — a global install (`npm i -g @answerlayer/cli`).
88
+ - `npx -y @answerlayer/cli` — fallback; downloads from npm on first use.
89
+
90
+ Use whichever it prints as `AL` below. They all run identical code.
91
+
92
+ ## 2. Ensure it's configured
93
+
94
+ The CLI needs a base URL (defaults to `https://app.answerlayer.io`) and an API
95
+ key sent as `X-API-Key`. Check whether credentials already exist:
96
+
97
+ ```bash
98
+ AL auth me --json
99
+ ```
100
+
101
+ - If it returns the caller's identity, you're configured — continue to step 3.
102
+ - If it errors with a missing-key/auth error, set credentials up:
103
+
104
+ 1. Ask the user for their **API key**. Also ask for their **host** only if
105
+ they're on a BYOC / self-hosted install; otherwise the default
106
+ `https://app.answerlayer.io` is correct.
107
+ 2. Configure (omit `--base-url` to accept the default):
108
+
109
+ ```bash
110
+ AL init --api-key <KEY> [--base-url https://answerlayer.their-host.com]
111
+ ```
112
+
113
+ This verifies the key before writing `~/.answerlayer/config.json` (mode
114
+ 0600). Alternatively the user can export `ANSWERLAYER_API_KEY` (and
115
+ optionally `ANSWERLAYER_BASE_URL`).
116
+ 3. Re-run `AL auth me --json` to confirm.
117
+
118
+ **Never print, echo, or log the API key.** Pass it straight to `init`. Tell
119
+ the user keys are created in the AnswerLayer dashboard under **Settings → API
120
+ Keys** and are shown only once.
121
+
122
+ ## 3. Use it
123
+
124
+ Most questions need a **connection** (a database). List them and grab an `id`:
125
+
126
+ ```bash
127
+ AL connections list # human table; add --json to parse
128
+ ```
129
+
130
+ ### Ask questions in natural language (preferred for analytical questions)
131
+
132
+ ```bash
133
+ # First question — use --json to capture the session id for follow-ups
134
+ AL inquiry ask --connection <connection-id> --json "What was revenue last month?"
135
+
136
+ # Follow-up in the same conversation (carries prior context)
137
+ AL inquiry ask --session <session-id> --json "Break that down by region"
138
+ ```
139
+
140
+ The JSON includes `session_id`, `final_response`, and `sql_queries`. Show the
141
+ user `final_response`; surface the SQL when they want to see how it was derived.
142
+ Without `--json` it prints the answer followed by the SQL as plain text.
143
+
144
+ ### Run SQL directly
145
+
146
+ ```bash
147
+ AL query run <connection-id> --sql "select count(*) from orders" --format table
148
+ AL query run <connection-id> --file ./report.sql --format csv
149
+ AL query validate <connection-id> --sql "select * from orders"
150
+ ```
151
+
152
+ ### Other resources
153
+
154
+ These follow `AL <group> <command>`. Run `AL <group> --help` (or `AL --help`)
155
+ to see exact flags before using one:
156
+
157
+ - `saved-queries` — list / get / create / execute reusable queries
158
+ - `semantic` — entities, relationships, measures, metrics, dimensions, filters
159
+ (most need `--connection <id>`)
160
+ - `dashboards`, `tiles` — build and read dashboards
161
+ - `documents` — upload business-context docs and link them to connections
162
+ - `connections` — create / test / inspect schema
163
+ - `metadata`, `api-keys`, `users`, `org`, `billing`, `stats`
164
+
165
+ ## Conventions
166
+
167
+ - Prefer `inquiry ask` for analytical/business questions; use `query run` only
168
+ when the user gives or wants explicit SQL.
169
+ - Add `--json` whenever you need to parse output programmatically; query results
170
+ also support `--format table|json|csv`.
171
+ - **Read freely, but confirm before writes.** `create`, `update`, `delete`,
172
+ `revoke`, `approve`, `deploy`, and `assign`/`unassign` change state — show the
173
+ user what you're about to run and get a yes first.
174
+ - If a command fails with a `403` / missing-scope error, the API key lacks a
175
+ scope (e.g. `inquiry:execute`, `query:execute`, `connection:read`). Tell the
176
+ user which scope to add to their key.
177
+ - For the full command surface, run `AL --help` or see the project README.
package/src/cli.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import fs from "node:fs";
2
+ import os from "node:os";
2
3
  import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
3
5
  import { AnswerLayerClient } from "./client.js";
4
6
  import { formatJson, formatList, formatQueryResult } from "./format.js";
5
7
  import { DEFAULT_BASE_URL, readConfig, resolveAuth, writeConfig } from "./config.js";
@@ -26,6 +28,14 @@ export async function main(argv, io) {
26
28
  return configure(parsed, io);
27
29
  }
28
30
 
31
+ if (group === "skills") {
32
+ return installSkill(command, parsed, io);
33
+ }
34
+
35
+ if (group === "init") {
36
+ return initialize(parsed, io);
37
+ }
38
+
29
39
  const { baseUrl, apiKey } = resolveAuth(parsed.flags, io.env);
30
40
  const client = new AnswerLayerClient({ baseUrl, apiKey });
31
41
 
@@ -69,6 +79,50 @@ function configure(parsed, io) {
69
79
  write(io.stdout, `Saved AnswerLayer config to ${configPath}\n`);
70
80
  }
71
81
 
82
+ function installSkill(command, parsed, io) {
83
+ if (command !== "install") {
84
+ throw usage("Expected `answerlayer skills install`");
85
+ }
86
+
87
+ const source = path.resolve(
88
+ path.dirname(fileURLToPath(import.meta.url)),
89
+ "../skills/answerlayer",
90
+ );
91
+ const codexHome = io.env.CODEX_HOME || path.join(os.homedir(), ".codex");
92
+ const target = firstValue(parsed.flags.path) || path.join(codexHome, "skills", "answerlayer");
93
+
94
+ if (fs.existsSync(target) && !parsed.flags.force) {
95
+ throw usage(
96
+ `Skill already exists at ${target}. Review it, then rerun with --force to replace it.`,
97
+ );
98
+ }
99
+
100
+ fs.mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
101
+ fs.cpSync(source, target, { recursive: true, force: true });
102
+ write(io.stdout, `Installed AnswerLayer skill to ${target}\n`);
103
+ }
104
+
105
+ async function initialize(parsed, io) {
106
+ const existing = readConfig(io.env);
107
+ const { baseUrl } = resolveAuth(parsed.flags, io.env);
108
+ const apiKey = firstValue(parsed.flags.apiKey);
109
+
110
+ if (!apiKey) {
111
+ throw usage(
112
+ "init requires an explicit --api-key. Use the command printed by `make local-bootstrap`, or create a key under Settings → API Keys.",
113
+ );
114
+ }
115
+
116
+ // Verify before writing so a typo or an expired key never replaces a working
117
+ // local configuration.
118
+ const client = new AnswerLayerClient({ baseUrl, apiKey });
119
+ await client.rawRequest("GET", "/api/v1/auth/me");
120
+
121
+ const configPath = writeConfig({ ...existing, baseUrl, apiKey }, io.env);
122
+ write(io.stdout, `Verified API key and saved AnswerLayer config to ${configPath}\n`);
123
+ write(io.stdout, "Next: answerlayer connections list\n");
124
+ }
125
+
72
126
  async function handleHealth(client, command, parsed, io) {
73
127
  const pathName = command === "ready" ? "/api/v1/ready" : "/api/v1/health";
74
128
  return requestAndPrint(client, "GET", pathName, parsed, io, { auth: false, okText: "ok" });
@@ -1196,7 +1250,7 @@ function normalizeFlagName(rawName) {
1196
1250
  }
1197
1251
 
1198
1252
  function isBooleanFlag(rawName) {
1199
- return ["--json", "--help", "-h", "--include", "-i", "--raw", "--admin"].includes(rawName);
1253
+ return ["--json", "--help", "-h", "--include", "-i", "--raw", "--admin", "--force"].includes(rawName);
1200
1254
  }
1201
1255
 
1202
1256
  function setFlag(flags, name, value) {
@@ -1282,6 +1336,8 @@ function helpText() {
1282
1336
  return `AnswerLayer CLI
1283
1337
 
1284
1338
  Usage:
1339
+ answerlayer skills install [--path <directory>] [--force]
1340
+ answerlayer init --api-key <key> [--base-url <url>]
1285
1341
  answerlayer configure --base-url <url> --api-key <key>
1286
1342
  answerlayer health
1287
1343
  answerlayer openapi --output openapi.json