@answerlayer/cli 0.2.4 → 0.2.5

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
@@ -83,6 +83,46 @@ export ANSWERLAYER_BASE_URL=https://answerlayer.your-company.com # optional ove
83
83
 
84
84
  The CLI sends API keys using the `X-API-Key` header.
85
85
 
86
+ ## Operate API pipelines
87
+
88
+ API pipeline authoring and lifecycle operations are CLI-first. The AnswerLayer
89
+ web application is a read-only observability surface for pipeline health, run
90
+ history, provenance, and failures.
91
+
92
+ Create a pipeline and upload an immutable package revision:
93
+
94
+ ```bash
95
+ answerlayer pipelines create --name "REMPLAN Census" --json
96
+ answerlayer pipelines revisions push <pipeline-id> \
97
+ --package ./dist/remplan-census.zip \
98
+ --config-file ./pipeline-config.json \
99
+ --json
100
+ ```
101
+
102
+ Validate and promote the returned revision ID. `--wait` polls the exact run to
103
+ a terminal state and exits unsuccessfully when validation fails or is
104
+ cancelled:
105
+
106
+ ```bash
107
+ answerlayer pipelines revisions validate <pipeline-id> <revision-id> --wait --json
108
+ answerlayer pipelines revisions promote <pipeline-id> <revision-id> --json
109
+ ```
110
+
111
+ Run and operate the promoted revision:
112
+
113
+ ```bash
114
+ answerlayer pipelines runs start <pipeline-id> --wait --json
115
+ answerlayer pipelines runs get <pipeline-id> <run-id> --json
116
+ answerlayer pipelines runs retry <pipeline-id> <run-id> --wait --json
117
+ answerlayer pipelines runs cancel <pipeline-id> <run-id> --json
118
+ answerlayer pipelines archive <pipeline-id> --json
119
+ ```
120
+
121
+ Use `--include-archived` with `pipelines list`. Package ZIPs are built from the
122
+ customer-owned package repository; credentials and CA material must not be
123
+ included in the ZIP or revision configuration. They remain in the install's
124
+ managed secret interfaces.
125
+
86
126
  ## Run locally with an agent
87
127
 
88
128
  The package includes an `answerlayer` agent skill for evaluating AnswerLayer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@answerlayer/cli",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Open-source command line client for the AnswerLayer API.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,6 +1,6 @@
1
1
  ---
2
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.
3
+ description: Query databases and operate customer-owned API pipelines 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, run SQL, or manage AnswerLayer API pipelines, connections, saved queries, the semantic layer, or dashboards.
4
4
  ---
5
5
 
6
6
  # AnswerLayer
@@ -180,6 +180,9 @@ to see exact flags before using one:
180
180
  - `dashboards`, `tiles` — build and read dashboards
181
181
  - `documents` — upload business-context docs and link them to connections
182
182
  - `connections` — create / test / inspect schema
183
+ - `pipelines` — upload immutable package revisions, validate and promote them,
184
+ run/retry/cancel exact revisions, inspect runs, and archive pipelines; use
185
+ `--wait --json` for automation and never put credentials in package config
183
186
  - `metadata`, `api-keys`, `users`, `org`, `billing`, `stats`
184
187
 
185
188
  ## Conventions
package/src/cli.js CHANGED
@@ -48,6 +48,7 @@ export async function main(argv, io) {
48
48
  if (group === "openapi") return handleOpenApi(client, parsed, io);
49
49
  if (group === "auth") return handleAuth(client, command, parsed, io);
50
50
  if (group === "api-keys" || group === "keys") return handleApiKeys(client, command, positionals, parsed, io);
51
+ if (group === "pipelines" || group === "pipeline") return handlePipelines(client, command, positionals, parsed, io);
51
52
  if (group === "connections") return handleConnections(client, command, positionals, parsed, io);
52
53
  if (group === "metadata") return handleMetadata(client, command, positionals, parsed, io);
53
54
  if (group === "query") return handleQuery(client, command, positionals, parsed, io);
@@ -185,6 +186,127 @@ async function handleApiKeys(client, command, positionals, parsed, io) {
185
186
  throw usage(`Unknown api-keys command: ${command}`);
186
187
  }
187
188
 
189
+ async function handlePipelines(client, command, positionals, parsed, io) {
190
+ const base = "/api/v1/api-pipelines";
191
+
192
+ if (command === "list" || !command) {
193
+ return requestAndPrint(client, "GET", base, parsed, io, {
194
+ query: { include_archived: Boolean(parsed.flags.includeArchived) },
195
+ table: [
196
+ { key: "id", label: "ID" },
197
+ { key: "name", label: "Name" },
198
+ { key: "status", label: "Status" },
199
+ { key: "active_revision_id", label: "Active revision" },
200
+ { key: "updated_at", label: "Updated" },
201
+ ],
202
+ });
203
+ }
204
+
205
+ if (command === "create") {
206
+ const payload = await readData(parsed.flags, io, {
207
+ name: firstValue(parsed.flags.name),
208
+ description: firstValue(parsed.flags.description),
209
+ });
210
+ requirePayloadValue(payload, "name", "pipelines create requires --name");
211
+ return requestAndPrint(client, "POST", base, parsed, io, { body: payload });
212
+ }
213
+
214
+ if (command === "archive") {
215
+ const pipelineId = requirePositional(positionals, 0, "pipeline id");
216
+ return requestAndPrint(
217
+ client,
218
+ "DELETE",
219
+ `${base}/${encodeURIComponent(pipelineId)}`,
220
+ parsed,
221
+ io,
222
+ );
223
+ }
224
+
225
+ if (command === "revisions") {
226
+ return handlePipelineRevisions(client, base, positionals, parsed, io);
227
+ }
228
+
229
+ if (command === "runs") {
230
+ return handlePipelineRuns(client, base, positionals, parsed, io);
231
+ }
232
+
233
+ throw usage(`Unknown pipelines command: ${command}`);
234
+ }
235
+
236
+ async function handlePipelineRevisions(client, base, positionals, parsed, io) {
237
+ const action = requirePositional(positionals, 0, "pipeline revisions action");
238
+ const pipelineId = requirePositional(positionals, 1, "pipeline id");
239
+ const pipelinePath = `${base}/${encodeURIComponent(pipelineId)}`;
240
+
241
+ if (action === "push" || action === "upload") {
242
+ const packagePath = firstValue(parsed.flags.package)
243
+ || requirePositional(positionals, 2, "pipeline package ZIP");
244
+ const config = readPipelineRevisionConfig(parsed.flags);
245
+ return requestAndPrint(client, "POST", `${pipelinePath}/revisions`, parsed, io, {
246
+ body: multipartFields({
247
+ files: { package: packagePath },
248
+ fields: { config_json: JSON.stringify(config) },
249
+ }),
250
+ });
251
+ }
252
+
253
+ const revisionId = requirePositional(positionals, 2, "pipeline revision id");
254
+ const revisionPath = `${pipelinePath}/revisions/${encodeURIComponent(revisionId)}`;
255
+ if (action === "validate") {
256
+ return requestWaitAndPrint(
257
+ client,
258
+ "POST",
259
+ `${revisionPath}/validate`,
260
+ parsed,
261
+ io,
262
+ pipelinePath,
263
+ );
264
+ }
265
+ if (action === "promote") {
266
+ return requestAndPrint(client, "POST", `${revisionPath}/promote`, parsed, io);
267
+ }
268
+
269
+ throw usage(`Unknown pipelines revisions command: ${action}`);
270
+ }
271
+
272
+ async function handlePipelineRuns(client, base, positionals, parsed, io) {
273
+ const action = requirePositional(positionals, 0, "pipeline runs action");
274
+ const pipelineId = requirePositional(positionals, 1, "pipeline id");
275
+ const pipelinePath = `${base}/${encodeURIComponent(pipelineId)}`;
276
+
277
+ if (action === "start" || action === "run") {
278
+ return requestWaitAndPrint(
279
+ client,
280
+ "POST",
281
+ `${pipelinePath}/runs`,
282
+ parsed,
283
+ io,
284
+ pipelinePath,
285
+ );
286
+ }
287
+
288
+ const runId = requirePositional(positionals, 2, "pipeline run id");
289
+ const runPath = `${pipelinePath}/runs/${encodeURIComponent(runId)}`;
290
+ if (action === "get") {
291
+ return requestAndPrint(client, "GET", runPath, parsed, io);
292
+ }
293
+ if (action === "retry") {
294
+ return requestWaitAndPrint(
295
+ client,
296
+ "POST",
297
+ `${runPath}/retry`,
298
+ parsed,
299
+ io,
300
+ pipelinePath,
301
+ );
302
+ }
303
+ if (action === "cancel") {
304
+ return requestAndPrint(client, "POST", `${runPath}/cancel`, parsed, io);
305
+ }
306
+
307
+ throw usage(`Unknown pipelines runs command: ${action}`);
308
+ }
309
+
188
310
  async function handleConnections(client, command, positionals, parsed, io) {
189
311
  if (command === "supported" || command === "supported-types") {
190
312
  return requestAndPrint(client, "GET", "/api/v1/connections/supported_types", parsed, io, {
@@ -1461,13 +1583,36 @@ async function readData(flags, io, defaults = {}) {
1461
1583
  return dropUndefined({ ...defaults, ...data });
1462
1584
  }
1463
1585
 
1586
+ function readPipelineRevisionConfig(flags) {
1587
+ const file = firstValue(flags.configFile);
1588
+ const inline = firstValue(flags.config);
1589
+ if (file && inline) {
1590
+ throw usage("pipelines revisions push accepts either --config-file or --config, not both");
1591
+ }
1592
+ const value = file
1593
+ ? parseJsonFlag(fs.readFileSync(file, "utf8"), "config-file")
1594
+ : inline
1595
+ ? parseJsonFlag(inline, "config")
1596
+ : {};
1597
+ if (!value || Array.isArray(value) || typeof value !== "object") {
1598
+ throw usage("pipeline revision config must be a JSON object");
1599
+ }
1600
+ return value;
1601
+ }
1602
+
1464
1603
  function multipart({ file, fields = {} }) {
1604
+ return multipartFields({ files: { file }, fields });
1605
+ }
1606
+
1607
+ function multipartFields({ files = {}, fields = {} }) {
1465
1608
  const form = new FormData();
1466
1609
  for (const [key, value] of Object.entries(dropUndefined(fields))) {
1467
1610
  form.append(key, String(value));
1468
1611
  }
1469
- const buffer = fs.readFileSync(file);
1470
- form.append("file", new Blob([buffer]), path.basename(file));
1612
+ for (const [key, file] of Object.entries(files)) {
1613
+ const buffer = fs.readFileSync(file);
1614
+ form.append(key, new Blob([buffer]), path.basename(file));
1615
+ }
1471
1616
  return form;
1472
1617
  }
1473
1618
 
@@ -1498,6 +1643,70 @@ async function requestAndPrint(client, method, pathName, parsed, io, options = {
1498
1643
  headers: options.headers,
1499
1644
  });
1500
1645
 
1646
+ return printResult(result, parsed, io, options);
1647
+ }
1648
+
1649
+ async function requestWaitAndPrint(
1650
+ client,
1651
+ method,
1652
+ pathName,
1653
+ parsed,
1654
+ io,
1655
+ pipelinePath,
1656
+ ) {
1657
+ let result = await client.rawRequest(method, pathName);
1658
+ if (parsed.flags.wait) {
1659
+ result = await waitForPipelineRun(client, result, parsed.flags, io, pipelinePath);
1660
+ }
1661
+ printResult(result, parsed, io);
1662
+ if (parsed.flags.wait && ["failed", "cancelled"].includes(result.data?.status)) {
1663
+ const error = new Error(
1664
+ result.data.error
1665
+ ? `Pipeline run ${result.data.id} ${result.data.status}: ${result.data.error}`
1666
+ : `Pipeline run ${result.data.id} ${result.data.status}`,
1667
+ );
1668
+ error.exitCode = 1;
1669
+ throw error;
1670
+ }
1671
+ }
1672
+
1673
+ async function waitForPipelineRun(client, initial, flags, io, pipelinePath) {
1674
+ const runId = initial.data?.id;
1675
+ if (!runId) throw new Error("Pipeline run response did not include an id");
1676
+ const terminal = new Set(["succeeded", "failed", "cancelled"]);
1677
+ const timeoutSeconds = parsePositiveNumber(
1678
+ firstValue(flags.waitTimeout),
1679
+ "wait-timeout",
1680
+ 600,
1681
+ );
1682
+ const pollSeconds = parsePositiveNumber(
1683
+ firstValue(flags.pollInterval),
1684
+ "poll-interval",
1685
+ 2,
1686
+ { allowZero: true },
1687
+ );
1688
+ const now = io.now || (() => Date.now());
1689
+ const sleep = io.sleep || (milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)));
1690
+ const deadline = now() + timeoutSeconds * 1000;
1691
+ let result = initial;
1692
+
1693
+ write(io.stderr, `Waiting for pipeline run ${runId}...\n`);
1694
+ while (!terminal.has(result.data?.status)) {
1695
+ if (now() >= deadline) {
1696
+ const error = new Error(`Timed out waiting for pipeline run ${runId}`);
1697
+ error.exitCode = 1;
1698
+ throw error;
1699
+ }
1700
+ await sleep(pollSeconds * 1000);
1701
+ result = await client.rawRequest(
1702
+ "GET",
1703
+ `${pipelinePath}/runs/${encodeURIComponent(runId)}`,
1704
+ );
1705
+ }
1706
+ return result;
1707
+ }
1708
+
1709
+ function printResult(result, parsed, io, options = {}) {
1501
1710
  const output = firstValue(parsed.flags.output);
1502
1711
  if (output) {
1503
1712
  fs.writeFileSync(output, result.buffer);
@@ -1607,6 +1816,12 @@ function normalizeFlagName(rawName) {
1607
1816
  "--type": "type",
1608
1817
  "--db-type": "dbType",
1609
1818
  "--config": "config",
1819
+ "--config-file": "configFile",
1820
+ "--package": "package",
1821
+ "--include-archived": "includeArchived",
1822
+ "--wait": "wait",
1823
+ "--wait-timeout": "waitTimeout",
1824
+ "--poll-interval": "pollInterval",
1610
1825
  "--file": "file",
1611
1826
  "-f": "file",
1612
1827
  "--sql": "sql",
@@ -1718,7 +1933,7 @@ function normalizeFlagName(rawName) {
1718
1933
  }
1719
1934
 
1720
1935
  function isBooleanFlag(rawName) {
1721
- return ["--json", "--help", "-h", "--include", "-i", "--raw", "--admin", "--force", "--follow", "--active", "--inactive", "--include-inactive", "--include-semantic-snapshot", "--use-semantic-layer", "--no-semantic-layer", "--no-demo", "--yes", "-y"].includes(rawName);
1936
+ return ["--json", "--help", "-h", "--include", "-i", "--raw", "--admin", "--force", "--follow", "--active", "--inactive", "--include-inactive", "--include-semantic-snapshot", "--include-archived", "--use-semantic-layer", "--no-semantic-layer", "--no-demo", "--yes", "-y", "--wait"].includes(rawName);
1722
1937
  }
1723
1938
 
1724
1939
  function setFlag(flags, name, value) {
@@ -1759,6 +1974,16 @@ function parseNumber(value, name) {
1759
1974
  return parsed;
1760
1975
  }
1761
1976
 
1977
+ function parsePositiveNumber(value, name, fallback, { allowZero = false } = {}) {
1978
+ if (value === undefined || value === null || value === "") return fallback;
1979
+ const parsed = Number(value);
1980
+ const minimum = allowZero ? 0 : Number.EPSILON;
1981
+ if (!Number.isFinite(parsed) || parsed < minimum) {
1982
+ throw usage(`Expected --${name} to be ${allowZero ? "zero or " : ""}greater than zero`);
1983
+ }
1984
+ return parsed;
1985
+ }
1986
+
1762
1987
  function parseBoundedInteger(value, name, minimum, maximum) {
1763
1988
  if (value === undefined || value === "") return undefined;
1764
1989
  if (typeof value !== "string" && typeof value !== "number") {
@@ -1888,6 +2113,9 @@ Usage:
1888
2113
 
1889
2114
  Core:
1890
2115
  answerlayer api-keys list|create|revoke
2116
+ answerlayer pipelines list|create|archive
2117
+ answerlayer pipelines revisions push|validate|promote <pipeline-id> [revision-id]
2118
+ answerlayer pipelines runs start|get|retry|cancel <pipeline-id> [run-id]
1891
2119
  answerlayer connections supported|list|get|create|update|delete|schema|test
1892
2120
  answerlayer metadata structure|tables|columns|pii-summary|pii-settings|detect-pii
1893
2121
  answerlayer query run|validate|export <connection-id> --sql <sql>
@@ -1952,6 +2180,15 @@ Common options:
1952
2180
  --data-file <path> Structured request payload file.
1953
2181
  --output, -o <path> Write response bytes to a file.
1954
2182
 
2183
+ Pipeline options:
2184
+ --package <path> Package ZIP for pipelines revisions push.
2185
+ --config-file <path> Non-secret revision configuration JSON file.
2186
+ --config <json> Inline non-secret revision configuration.
2187
+ --include-archived Include archived pipelines when listing.
2188
+ --wait Poll validation or execution to a terminal state.
2189
+ --wait-timeout <sec> Maximum wait time. Default: 600.
2190
+ --poll-interval <sec> Poll interval. Default: 2.
2191
+
1955
2192
  SQL options:
1956
2193
  --sql, -q <sql> SQL text.
1957
2194
  --file, -f <path> Read SQL from a file, or upload a file for upload commands.