@sequenceholdings/studio-cli 0.7.0 → 0.7.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 +68 -8
- package/dist/app/commands.d.ts +3 -1
- package/dist/app/commands.js +51 -0
- package/dist/app/validate.d.ts +16 -0
- package/dist/app/validate.js +40 -0
- package/dist/atlas-client.d.ts +2 -0
- package/dist/atlas-client.js +8 -0
- package/dist/bin.js +2 -1
- package/dist/cli-errors.js +8 -3
- package/dist/command-contract.d.ts +6 -0
- package/dist/command-contract.js +1 -0
- package/dist/command-help.d.ts +4 -3
- package/dist/command-help.js +39 -3
- package/dist/functions/commands.d.ts +4 -0
- package/dist/functions/commands.js +61 -0
- package/dist/main.js +5 -0
- package/dist/orm/delegate.d.ts +1 -0
- package/dist/orm/delegate.js +40 -14
- package/dist/orm/discover-schema.d.ts +98 -0
- package/dist/orm/discover-schema.js +32 -0
- package/dist/orm/discover.js +28 -3
- package/dist/orm/execute.d.ts +33 -0
- package/dist/orm/execute.js +208 -0
- package/dist/orm/operation-show.d.ts +2 -0
- package/dist/orm/operation-show.js +35 -0
- package/dist/orm/read.js +2 -2
- package/dist/transient-503.d.ts +4 -0
- package/dist/transient-503.js +25 -0
- package/package.json +5 -4
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `seq-studio orm execute <namespace> <operation>` — run a deployed persisted
|
|
3
|
+
* operation by name, with no checkout.
|
|
4
|
+
*
|
|
5
|
+
* The data endpoint selects an operation by content hash, so a checkout's
|
|
6
|
+
* manifest only works while it matches what is deployed. This form asks the
|
|
7
|
+
* control plane for the ACTIVE version's operation (hash and declared
|
|
8
|
+
* variables) and posts that hash, so it always runs what is deployed. The
|
|
9
|
+
* server still applies the operation's requirements and the namespace's row
|
|
10
|
+
* policies to the caller.
|
|
11
|
+
*/
|
|
12
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
13
|
+
import { resolve } from 'node:path';
|
|
14
|
+
import { getJsonOr404, postJsonResponse } from '../atlas-client.js';
|
|
15
|
+
import { printCliError } from '../cli-errors.js';
|
|
16
|
+
import { validateCommandInput } from '../command-input.js';
|
|
17
|
+
import { buildContext, clientOptions } from '../functions/commands.js';
|
|
18
|
+
import { operationDetailSchema, operationsPageSchema, parseResponse, } from './discover-schema.js';
|
|
19
|
+
/** The operation detail route's name grammar (a GraphQL name). */
|
|
20
|
+
const OPERATION_NAME = /^[_A-Za-z][_0-9A-Za-z]{0,254}$/;
|
|
21
|
+
/** The server's namespace grammar, without its reserved names. */
|
|
22
|
+
const NAMESPACE_NAME = /^[a-z][a-z0-9_]{0,40}$/;
|
|
23
|
+
const LISTED_NAMES = 20;
|
|
24
|
+
const NUMBER_SCALARS = new Set(['integer', 'double']);
|
|
25
|
+
const JSON_SCALARS = new Set(['jsonb']);
|
|
26
|
+
/**
|
|
27
|
+
* `<namespace> <operation>` when the second argument is an operation name and
|
|
28
|
+
* not a directory; otherwise the checkout form `<operation> [dir]`.
|
|
29
|
+
*/
|
|
30
|
+
export async function isDeployedExecute(args) {
|
|
31
|
+
const [, second] = args.positional;
|
|
32
|
+
if (args.positional.length !== 2 || second === undefined || !OPERATION_NAME.test(second))
|
|
33
|
+
return false;
|
|
34
|
+
return !(await stat(second).catch(() => undefined))?.isDirectory();
|
|
35
|
+
}
|
|
36
|
+
/** `id: uuid!`, `ids: [uuid]`, `where: boolExp(loans)`, `limit: integer = 50`. */
|
|
37
|
+
export function variableLabel(variable) {
|
|
38
|
+
const { type } = variable;
|
|
39
|
+
const base = type.kind === 'scalar' ? type.scalar
|
|
40
|
+
: type.kind === 'enum' ? type.name
|
|
41
|
+
: type.kind === 'inputRef' ? `${type.input}(${type.table})`
|
|
42
|
+
: undefined;
|
|
43
|
+
const shown = variable.list ? `[${base ?? type.kind}]` : (base ?? type.kind);
|
|
44
|
+
const suffix = 'default' in variable ? ` = ${JSON.stringify(variable.default)}` : variable.nullable ? '' : '!';
|
|
45
|
+
return `${variable.name}: ${shown}${suffix}`;
|
|
46
|
+
}
|
|
47
|
+
export function variablesLabel(variables) {
|
|
48
|
+
return variables.map(variableLabel).join(', ');
|
|
49
|
+
}
|
|
50
|
+
function takesJson(variable) {
|
|
51
|
+
return variable.list || variable.type.kind === 'inputRef' || JSON_SCALARS.has(variable.type.scalar ?? '');
|
|
52
|
+
}
|
|
53
|
+
/** One `--var` value, typed by the deployed operation's declaration. */
|
|
54
|
+
export function coerceVariable({ variable, raw }) {
|
|
55
|
+
const label = `--var ${variable.name} (${variableLabel(variable)})`;
|
|
56
|
+
if (takesJson(variable)) {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(raw);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new Error(`${label} takes JSON, e.g. --var '${variable.name}=${variable.list ? '["a","b"]' : '{"status":{"_eq":"open"}}'}'.`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const scalar = variable.type.kind === 'scalar' ? variable.type.scalar : undefined;
|
|
65
|
+
if (scalar === 'boolean') {
|
|
66
|
+
if (raw !== 'true' && raw !== 'false')
|
|
67
|
+
throw new Error(`${label} must be true or false.`);
|
|
68
|
+
return raw === 'true';
|
|
69
|
+
}
|
|
70
|
+
if (scalar !== undefined && NUMBER_SCALARS.has(scalar)) {
|
|
71
|
+
const value = Number(raw);
|
|
72
|
+
if (raw.trim() === '' || !Number.isFinite(value) || (scalar === 'integer' && !Number.isSafeInteger(value))) {
|
|
73
|
+
throw new Error(`${label}: '${raw}' is not a valid ${scalar}.`);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
// Text-like scalars, enums, and bigint/numeric (sent as lossless decimal strings).
|
|
78
|
+
return raw;
|
|
79
|
+
}
|
|
80
|
+
export function parseVarFlags({ args, operation }) {
|
|
81
|
+
const entries = args.repeatedFlags?.var ?? [];
|
|
82
|
+
const declared = new Map(operation.variables.map((variable) => [variable.name, variable]));
|
|
83
|
+
const variables = {};
|
|
84
|
+
for (const entry of entries) {
|
|
85
|
+
const eq = entry.indexOf('=');
|
|
86
|
+
const name = eq > 0 ? entry.slice(0, eq) : '';
|
|
87
|
+
if (!name)
|
|
88
|
+
throw new Error(`Invalid --var '${entry}': expected <name>=<value>.`);
|
|
89
|
+
const variable = declared.get(name);
|
|
90
|
+
if (!variable) {
|
|
91
|
+
const known = operation.variables.length ? variablesLabel(operation.variables) : 'no variables';
|
|
92
|
+
throw new Error(`${operation.name} has no variable '${name}'. It declares: ${known}.`);
|
|
93
|
+
}
|
|
94
|
+
if (Object.hasOwn(variables, name))
|
|
95
|
+
throw new Error(`--var ${name} is given more than once.`);
|
|
96
|
+
variables[name] = coerceVariable({ variable, raw: entry.slice(eq + 1) });
|
|
97
|
+
}
|
|
98
|
+
return variables;
|
|
99
|
+
}
|
|
100
|
+
async function readVariablesFile(args) {
|
|
101
|
+
const file = args.flags['variables-file'];
|
|
102
|
+
if (typeof file !== 'string')
|
|
103
|
+
return {};
|
|
104
|
+
let parsed;
|
|
105
|
+
try {
|
|
106
|
+
parsed = JSON.parse(await readFile(resolve(file), 'utf8'));
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw new Error(`Could not read JSON variables from ${file}: ${error instanceof Error ? error.message : String(error)}`);
|
|
110
|
+
}
|
|
111
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
112
|
+
throw new Error('--variables-file must contain a JSON object.');
|
|
113
|
+
return parsed;
|
|
114
|
+
}
|
|
115
|
+
/** The ACTIVE version's operation, or an error that says what is deployed instead. */
|
|
116
|
+
async function resolveDeployedOperation({ options, namespace, operation }) {
|
|
117
|
+
const encoded = encodeURIComponent(namespace);
|
|
118
|
+
const detail = await getJsonOr404({
|
|
119
|
+
...options,
|
|
120
|
+
path: `/api/orm/namespaces/${encoded}/operations/${encodeURIComponent(operation)}`,
|
|
121
|
+
});
|
|
122
|
+
if (detail !== null)
|
|
123
|
+
return parseResponse({ schema: operationDetailSchema, body: detail, what: 'operation' });
|
|
124
|
+
const listing = await getJsonOr404({ ...options, path: `/api/orm/namespaces/${encoded}/operations?limit=500` });
|
|
125
|
+
if (listing === null) {
|
|
126
|
+
throw new Error(`Namespace '${namespace}' was not found, has no applied version, or you cannot read it. ` +
|
|
127
|
+
'List the namespaces you can read: seq-studio orm namespaces');
|
|
128
|
+
}
|
|
129
|
+
const page = parseResponse({ schema: operationsPageSchema, body: listing, what: 'operations' });
|
|
130
|
+
throw new Error(missingOperationMessage({ namespace, operation, page }));
|
|
131
|
+
}
|
|
132
|
+
function missingOperationMessage({ namespace, operation, page }) {
|
|
133
|
+
if (page.total === 0) {
|
|
134
|
+
return `Namespace '${namespace}' has no deployed persisted operations. Apply its definitions first: seq-studio orm apply`;
|
|
135
|
+
}
|
|
136
|
+
const names = page.operations.map((listed) => listed.name);
|
|
137
|
+
const sameName = names.find((name) => name.toLowerCase() === operation.toLowerCase());
|
|
138
|
+
const version = page.schema_version_id ? ` (version ${page.schema_version_id})` : '';
|
|
139
|
+
const shown = names.slice(0, LISTED_NAMES).join(', ') + (page.total > LISTED_NAMES ? ', …' : '');
|
|
140
|
+
return [
|
|
141
|
+
`No operation '${operation}' in ${namespace}'s deployed version${version}.`,
|
|
142
|
+
...(sameName ? [`Did you mean ${sameName}? Operation names are case-sensitive.`] : []),
|
|
143
|
+
`Deployed operations: ${shown}`,
|
|
144
|
+
`List them with their variables: seq-studio orm describe ${namespace} --operations`,
|
|
145
|
+
].join('\n');
|
|
146
|
+
}
|
|
147
|
+
function parseEnvelope(body) {
|
|
148
|
+
try {
|
|
149
|
+
const parsed = JSON.parse(body);
|
|
150
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function failureMessage({ namespace, operation, status, envelope }) {
|
|
157
|
+
const first = Array.isArray(envelope?.errors) ? envelope.errors[0] : undefined;
|
|
158
|
+
const code = typeof first?.extensions?.code === 'string' ? first.extensions.code : null;
|
|
159
|
+
const detail = typeof first?.message === 'string' ? `${first.message}${code ? ` (${code})` : ''}` : `HTTP ${status}`;
|
|
160
|
+
const lines = [`${namespace}.${operation.name} failed: ${detail}`];
|
|
161
|
+
if (code === 'BAD_USER_INPUT' && operation.variables.length > 0) {
|
|
162
|
+
lines.push(`Variables: ${variablesLabel(operation.variables)}`);
|
|
163
|
+
}
|
|
164
|
+
if ((code === 'FORBIDDEN' || status === 403) && operation.requires) {
|
|
165
|
+
const { capability, role } = operation.requires;
|
|
166
|
+
// The server usually names the requirement itself; add it only when it does not.
|
|
167
|
+
const named = [capability, role].some((requirement) => requirement && detail.includes(requirement));
|
|
168
|
+
if (!named) {
|
|
169
|
+
lines.push(`It requires ${[capability ? `capability ${capability}` : null, role ? `role ${role}` : null].filter(Boolean).join(' and ')}.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (status === 404) {
|
|
173
|
+
lines.push('The deployed operation was not found when it ran; the namespace may have just been redeployed. Retry.');
|
|
174
|
+
}
|
|
175
|
+
return lines.join('\n');
|
|
176
|
+
}
|
|
177
|
+
export async function ormExecuteDeployedCommand(args) {
|
|
178
|
+
try {
|
|
179
|
+
validateCommandInput({ args, flags: { 'variables-file': 'string', var: 'string' }, positional: 2 });
|
|
180
|
+
const [namespace, operationName] = args.positional;
|
|
181
|
+
if (!NAMESPACE_NAME.test(namespace)) {
|
|
182
|
+
throw new Error(`'${namespace}' is not a namespace name and '${operationName}' is not a directory. ` +
|
|
183
|
+
'Run a deployed operation with: seq-studio orm execute <namespace> <operation>; ' +
|
|
184
|
+
'from a checkout: seq-studio orm execute <operation> [dir].');
|
|
185
|
+
}
|
|
186
|
+
const fileVariables = await readVariablesFile(args);
|
|
187
|
+
const options = clientOptions(await buildContext(args));
|
|
188
|
+
const operation = await resolveDeployedOperation({ options, namespace, operation: operationName });
|
|
189
|
+
const variables = { ...fileVariables, ...parseVarFlags({ args, operation }) };
|
|
190
|
+
const response = await postJsonResponse({
|
|
191
|
+
...options,
|
|
192
|
+
path: `/api/data/v2/${namespace}`,
|
|
193
|
+
body: { op: operation.op_hash, variables },
|
|
194
|
+
});
|
|
195
|
+
const envelope = parseEnvelope(response.body);
|
|
196
|
+
const ok = response.status >= 200 && response.status < 300 && envelope !== null;
|
|
197
|
+
// A GraphQL envelope can carry partial data alongside errors: print it, then fail.
|
|
198
|
+
if (ok)
|
|
199
|
+
console.log(JSON.stringify(envelope, null, 2));
|
|
200
|
+
if (ok && !(Array.isArray(envelope.errors) && envelope.errors.length > 0))
|
|
201
|
+
return 0;
|
|
202
|
+
throw new Error(failureMessage({ namespace, operation, status: response.status, envelope }));
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
printCliError(error);
|
|
206
|
+
return 1;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** Inspect the active persisted operation without executing it. */
|
|
2
|
+
import { AtlasApiError, getJson } from '../atlas-client.js';
|
|
3
|
+
import { printCliError } from '../cli-errors.js';
|
|
4
|
+
import { validateCommandInput } from '../command-input.js';
|
|
5
|
+
import { buildContext, clientOptions } from '../functions/commands.js';
|
|
6
|
+
import { operationInspectionSchema, parseResponse } from './discover-schema.js';
|
|
7
|
+
export async function ormOperationShowCommand(args) {
|
|
8
|
+
try {
|
|
9
|
+
validateCommandInput({ args, flags: {}, positional: 3 });
|
|
10
|
+
const [action, namespace, name] = args.positional;
|
|
11
|
+
if (action !== 'show')
|
|
12
|
+
throw new Error('Expected orm operation show <namespace> <name>. See --help.');
|
|
13
|
+
if (!namespace || !name)
|
|
14
|
+
throw new Error('Namespace and operation name are required.');
|
|
15
|
+
const options = clientOptions(await buildContext(args));
|
|
16
|
+
const path = `/api/orm/namespaces/${encodeURIComponent(namespace)}/operations/${encodeURIComponent(name)}`;
|
|
17
|
+
let body;
|
|
18
|
+
try {
|
|
19
|
+
body = await getJson({ ...options, path });
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error instanceof AtlasApiError && error.status === 404) {
|
|
23
|
+
throw new Error(`Operation '${namespace}/${name}' was not found or you cannot read it.`);
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
const operation = parseResponse({ schema: operationInspectionSchema, body, what: 'operation detail' });
|
|
28
|
+
console.log(args.flags.json === true ? JSON.stringify(operation, null, 2) : operation.document);
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
printCliError(error);
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/dist/orm/read.js
CHANGED
|
@@ -192,8 +192,8 @@ export function buildReadSql({ plan, columnTypes }) {
|
|
|
192
192
|
/** The ORM client's operation transport, served by the CLI's Atlas client. */
|
|
193
193
|
function atlasTransport({ baseUrl, token }) {
|
|
194
194
|
return async ({ namespace, op, variables }) => {
|
|
195
|
-
//
|
|
196
|
-
const response = await postJsonResponse({ baseUrl, token, path: `/
|
|
195
|
+
// The data endpoint `orm execute` uses; it shares the deployment gate of the /api/orm discovery routes.
|
|
196
|
+
const response = await postJsonResponse({ baseUrl, token, path: `/api/data/v2/${namespace}`, body: { op, variables } });
|
|
197
197
|
let body = null;
|
|
198
198
|
try {
|
|
199
199
|
body = JSON.parse(response.body);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The structured `error.code` (or top-level `code`) of an Atlas error body, if any. */
|
|
2
|
+
export declare function errorCode(body: unknown): string | null;
|
|
3
|
+
/** Whether a 503 may clear if retried: anything but a known configuration error. */
|
|
4
|
+
export declare function isTransient503(body: unknown): boolean;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured 503 codes that are a standing answer, not a warm-up: the feature
|
|
3
|
+
* is not configured for this environment, so retrying only delays the error.
|
|
4
|
+
* Atlas also sends plain `{ detail }` 503s for transient states without a
|
|
5
|
+
* Retry-After (for example a deploy-permission check that is briefly
|
|
6
|
+
* unavailable), so only a code listed here opts a 503 out of retrying.
|
|
7
|
+
*/
|
|
8
|
+
const PERMANENT_503_CODES = new Set(['storage_configuration']);
|
|
9
|
+
/** The structured `error.code` (or top-level `code`) of an Atlas error body, if any. */
|
|
10
|
+
export function errorCode(body) {
|
|
11
|
+
if (!body || typeof body !== 'object')
|
|
12
|
+
return null;
|
|
13
|
+
const { code, error } = body;
|
|
14
|
+
if (error && typeof error === 'object') {
|
|
15
|
+
const nested = error.code;
|
|
16
|
+
if (typeof nested === 'string')
|
|
17
|
+
return nested;
|
|
18
|
+
}
|
|
19
|
+
return typeof code === 'string' ? code : null;
|
|
20
|
+
}
|
|
21
|
+
/** Whether a 503 may clear if retried: anything but a known configuration error. */
|
|
22
|
+
export function isTransient503(body) {
|
|
23
|
+
const code = errorCode(body);
|
|
24
|
+
return code === null || !PERMANENT_503_CODES.has(code);
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sequenceholdings/studio-cli",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Unified Sequence Studio CLI — `seq-studio init` / `add` / `deploy` (app monorepos), `seq-studio agents`, `seq-studio process` (Lattice), `seq-studio artifact`, `seq-studio functions` / `secrets`, `seq-studio repos`, and `seq-studio auth whoami`. Includes Auth0 browser login shared with seqapi.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -41,12 +41,13 @@
|
|
|
41
41
|
"smol-toml": "^1.9.0",
|
|
42
42
|
"tsx": "^4.20.3",
|
|
43
43
|
"zod": "^4.1.13",
|
|
44
|
+
"drizzle-orm": "^0.45.2",
|
|
44
45
|
"yaml": "^2.8.4",
|
|
45
|
-
"@sequenceholdings/managed-functions": "0.4.5",
|
|
46
|
-
"@sequenceholdings/storage": "0.1.2",
|
|
47
46
|
"@sequenceholdings/agent-spec": "0.1.4",
|
|
48
47
|
"@sequenceholdings/artifact-studio": "0.10.1",
|
|
49
|
-
"@sequenceholdings/lattice": "0.1.7"
|
|
48
|
+
"@sequenceholdings/lattice": "0.1.7",
|
|
49
|
+
"@sequenceholdings/managed-functions": "0.4.5",
|
|
50
|
+
"@sequenceholdings/storage": "0.1.2"
|
|
50
51
|
},
|
|
51
52
|
"peerDependencies": {
|
|
52
53
|
"@sequenceholdings/orm": "2.7.1",
|