@agentic-kit/db-tools 0.15.1 → 0.16.0
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 +4 -4
- package/blueprint/blueprint-schema.js +3 -1
- package/context.d.ts +9 -9
- package/context.js +92 -54
- package/db-probe.js +2 -2
- package/esm/blueprint/blueprint-schema.js +3 -1
- package/esm/context.d.ts +9 -9
- package/esm/context.js +91 -54
- package/esm/db-probe.js +2 -2
- package/esm/failure.d.ts +1 -1
- package/esm/failure.js +2 -2
- package/esm/gate.js +1 -1
- package/esm/graphql-http.d.ts +3 -2
- package/esm/graphql-http.js +26 -7
- package/esm/host.d.ts +20 -7
- package/esm/host.js +7 -2
- package/esm/index.d.ts +175 -14
- package/esm/index.js +5 -15
- package/esm/project-file.d.ts +72 -0
- package/esm/project-file.js +161 -0
- package/esm/provision-database/provision.d.ts +8 -3
- package/esm/provision-database/provision.js +63 -94
- package/esm/ready.d.ts +7 -3
- package/esm/ready.js +3 -3
- package/esm/records/rows.d.ts +1 -0
- package/esm/records/rows.js +9 -6
- package/esm/tools/access.js +1 -1
- package/esm/tools/create-api-key.js +2 -2
- package/esm/tools/delete-records.d.ts +1 -1
- package/esm/tools/delete-records.js +7 -7
- package/esm/tools/index.d.ts +195 -2
- package/esm/tools/index.js +25 -15
- package/esm/tools/mutations.js +7 -6
- package/esm/tools/provision-database.js +4 -4
- package/esm/tools/run-codegen.d.ts +2 -2
- package/esm/tools/run-codegen.js +10 -11
- package/esm/tools/update-records.js +1 -1
- package/failure.d.ts +1 -1
- package/failure.js +2 -2
- package/gate.js +1 -1
- package/graphql-http.d.ts +3 -2
- package/graphql-http.js +26 -7
- package/host.d.ts +20 -7
- package/host.js +7 -2
- package/index.d.ts +175 -14
- package/index.js +9 -45
- package/package.json +19 -16
- package/project-file.d.ts +72 -0
- package/project-file.js +178 -0
- package/provision-database/provision.d.ts +8 -3
- package/provision-database/provision.js +66 -96
- package/ready.d.ts +7 -3
- package/ready.js +3 -3
- package/records/rows.d.ts +1 -0
- package/records/rows.js +8 -5
- package/tools/access.js +1 -1
- package/tools/create-api-key.js +2 -2
- package/tools/delete-records.d.ts +1 -1
- package/tools/delete-records.js +8 -8
- package/tools/index.d.ts +195 -2
- package/tools/index.js +53 -43
- package/tools/mutations.js +6 -5
- package/tools/provision-database.js +5 -5
- package/tools/run-codegen.d.ts +2 -2
- package/tools/run-codegen.js +8 -9
- package/tools/update-records.js +1 -1
- package/esm/provision-database/env-file.d.ts +0 -62
- package/esm/provision-database/env-file.js +0 -170
- package/esm/provision-database/pending-ticket.d.ts +0 -33
- package/esm/provision-database/pending-ticket.js +0 -63
- package/provision-database/env-file.d.ts +0 -62
- package/provision-database/env-file.js +0 -179
- package/provision-database/pending-ticket.d.ts +0 -33
- package/provision-database/pending-ticket.js +0 -72
package/esm/context.js
CHANGED
|
@@ -5,15 +5,15 @@ import { parseDotenv } from '12factor-env/dotenv';
|
|
|
5
5
|
import { probeDatabase } from './db-probe';
|
|
6
6
|
import { failureFromError, failureFromErrors, failureText, invalidBinding, NEW_DATABASE, noBackendFailure } from './failure';
|
|
7
7
|
import { DEFAULT_DATA_TOKEN_SKEW_MS, getHost, readAccount, readBackendConfig, replaceCall, reprovisionAdvice, } from './host';
|
|
8
|
-
import {
|
|
8
|
+
import { isDatabaseName, isEndpoint, isUuid, PROJECT_FILE, readProjectFile } from './project-file';
|
|
9
9
|
// The failure's code is `code` unless `why.code` names a more precise one.
|
|
10
10
|
function unresolved(code, why, cause) {
|
|
11
11
|
const failure = { ...why, code: why.code ?? code };
|
|
12
12
|
return { context: null, reason: failureText(failure), code, ...(cause ? { cause } : {}), failure };
|
|
13
13
|
}
|
|
14
|
-
// The project values pi needs, and where they come from. A
|
|
15
|
-
//
|
|
16
|
-
// a Job, CI — injects values without writing
|
|
14
|
+
// The project values pi needs, and where they come from. A project folder
|
|
15
|
+
// carries its binding in `.constructive/project.json`; a headless host — a
|
|
16
|
+
// container, a Job, CI — injects these values without writing them into a git clone.
|
|
17
17
|
export const CONTEXT_ENV_KEYS = [
|
|
18
18
|
'ACCESS_TOKEN',
|
|
19
19
|
'DATABASE_ID',
|
|
@@ -23,9 +23,8 @@ export const CONTEXT_ENV_KEYS = [
|
|
|
23
23
|
'OWNER_ID',
|
|
24
24
|
];
|
|
25
25
|
/** Prefix for injected variables: `ACCESS_TOKEN` is far too generic to claim in
|
|
26
|
-
* a shared process environment, `CONSTRUCTIVE_ACCESS_TOKEN` is not.
|
|
27
|
-
*
|
|
28
|
-
* writes), so both spellings resolve, prefixed winning. */
|
|
26
|
+
* a shared process environment, `CONSTRUCTIVE_ACCESS_TOKEN` is not. Both
|
|
27
|
+
* spellings resolve, prefixed winning. */
|
|
29
28
|
export const CONTEXT_ENV_PREFIX = 'CONSTRUCTIVE_';
|
|
30
29
|
const lookup = (source) => typeof source === 'function' ? source : (name) => source[name];
|
|
31
30
|
const readContextValue = (source, key) => {
|
|
@@ -37,54 +36,70 @@ const readContextValue = (source, key) => {
|
|
|
37
36
|
export function fromEnvironment(environment = process.env) {
|
|
38
37
|
return environment;
|
|
39
38
|
}
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
export function readEnv(content) {
|
|
40
|
+
return parseDotenv(content);
|
|
41
|
+
}
|
|
42
42
|
export async function fromEnvFile(cwd) {
|
|
43
43
|
try {
|
|
44
|
-
return
|
|
44
|
+
return readEnv(await readFile(path.join(cwd, '.env'), 'utf8'));
|
|
45
45
|
}
|
|
46
46
|
catch {
|
|
47
47
|
return null;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
+
/** The value checks of an injected context: a value that is not of its form is never used, sent or shown. */
|
|
51
|
+
const CONTEXT_FORMS = {
|
|
52
|
+
DATABASE_ID: isUuid,
|
|
53
|
+
OWNER_ID: isUuid,
|
|
54
|
+
DATABASE_NAME: isDatabaseName,
|
|
55
|
+
API_ENDPOINT: isEndpoint,
|
|
56
|
+
MODULES_ENDPOINT: isEndpoint,
|
|
57
|
+
};
|
|
58
|
+
const invalidContextKeys = (env) => Object.entries(CONTEXT_FORMS).flatMap(([key, valid]) => (env[key] && !valid(env[key]) ? [key] : []));
|
|
50
59
|
/**
|
|
51
60
|
* Resolve the project context from injected values, or from a project folder.
|
|
52
61
|
*
|
|
53
|
-
* Passing a `cwd` string reads the binding in `<cwd>/.
|
|
54
|
-
* through the host.
|
|
62
|
+
* Passing a `cwd` string reads the binding in `<cwd>/.constructive/project.json`
|
|
63
|
+
* and authenticates through the host. That file names no endpoint and holds no
|
|
64
|
+
* token: every request goes to the host's backend.
|
|
55
65
|
* Headless hosts pass values instead: `fromEnvironment()` for a container or
|
|
56
66
|
* Job, an explicit record for anything else.
|
|
57
67
|
*/
|
|
58
68
|
export async function resolveProjectContext(input, options = {}) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return unresolved('missing-credentials', invalidBinding(invalid, fileBacked ? '.env' : 'the injected project context'));
|
|
72
|
-
const accessToken = env.ACCESS_TOKEN;
|
|
73
|
-
const databaseId = env.DATABASE_ID;
|
|
74
|
-
if (!databaseId || (!fileBacked && !accessToken)) {
|
|
75
|
-
return unresolved('missing-credentials', fileBacked
|
|
76
|
-
? {
|
|
77
|
-
message: 'Project is not connected to a Constructive database yet (missing DATABASE_ID in .env).',
|
|
78
|
-
hint: 'Bind this directory to a database (the person approves it), then retry.',
|
|
69
|
+
let env;
|
|
70
|
+
// The backend the binding was made on: compared with the host's, never sent to.
|
|
71
|
+
let boundBackend;
|
|
72
|
+
if (typeof input === 'string') {
|
|
73
|
+
const file = await readProjectFile(input);
|
|
74
|
+
if (file.kind === 'failed')
|
|
75
|
+
return unresolved('missing-credentials', file.failure);
|
|
76
|
+
const database = file.value?.database;
|
|
77
|
+
if (!database) {
|
|
78
|
+
return unresolved('not-bound', {
|
|
79
|
+
message: `This directory is not bound to a database yet (no binding in ${PROJECT_FILE}).`,
|
|
80
|
+
hint: 'Create or bind a database for this directory (the person approves it), then retry.',
|
|
79
81
|
calls: [NEW_DATABASE],
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
env = { ACCESS_TOKEN: undefined, DATABASE_ID: database.id, API_ENDPOINT: undefined, MODULES_ENDPOINT: undefined, DATABASE_NAME: database.name, OWNER_ID: database.ownerId };
|
|
85
|
+
boundBackend = file.value?.backend;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
env = Object.fromEntries(CONTEXT_ENV_KEYS.map((key) => [key, readContextValue(input, key)]));
|
|
89
|
+
const invalid = invalidContextKeys(env);
|
|
90
|
+
if (invalid.length)
|
|
91
|
+
return unresolved('missing-credentials', invalidBinding(invalid, 'the injected project context'));
|
|
92
|
+
if (!env.ACCESS_TOKEN || !env.DATABASE_ID) {
|
|
93
|
+
return unresolved('missing-credentials', {
|
|
82
94
|
message: `Missing ${CONTEXT_ENV_PREFIX}ACCESS_TOKEN or ${CONTEXT_ENV_PREFIX}DATABASE_ID from the injected project context.`,
|
|
83
95
|
});
|
|
96
|
+
}
|
|
84
97
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
98
|
+
const accessToken = env.ACCESS_TOKEN;
|
|
99
|
+
const databaseId = env.DATABASE_ID;
|
|
100
|
+
// An injected pin wins for the DATA plane (the access key belongs to whatever
|
|
101
|
+
// backend the host injected it for); otherwise the host's backend config,
|
|
102
|
+
// the only endpoint source of a project folder.
|
|
88
103
|
const host = getHost();
|
|
89
104
|
const backendRead = readBackendConfig(host);
|
|
90
105
|
if (backendRead.kind === 'failed')
|
|
@@ -96,9 +111,9 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
96
111
|
// tools) authenticates with the ACCOUNT bearer: the platform api rejects
|
|
97
112
|
// per-database keys, so the project ACCESS_TOKEN can never act on metaschema
|
|
98
113
|
// surfaces. An explicitly injected key stays in the context for the data plane only.
|
|
99
|
-
// The bearer is only ever sent to the app-configured backend
|
|
100
|
-
//
|
|
101
|
-
const accountRead = await readAccount(host);
|
|
114
|
+
// The bearer is only ever sent to the app-configured backend, never to an
|
|
115
|
+
// injected pin.
|
|
116
|
+
const accountRead = await readAccount(host, backend);
|
|
102
117
|
if (accountRead.kind === 'failed')
|
|
103
118
|
return unresolved('missing-credentials', accountRead.failure);
|
|
104
119
|
const account = accountRead.value;
|
|
@@ -113,6 +128,13 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
113
128
|
if (!controlApiEndpoint || !controlModulesEndpoint) {
|
|
114
129
|
return unresolved('no-backend', noBackendFailure(host.signInHint));
|
|
115
130
|
}
|
|
131
|
+
if (boundBackend && boundBackend !== controlApiEndpoint) {
|
|
132
|
+
return unresolved('db-missing', {
|
|
133
|
+
message: `This directory is bound to ${env.DATABASE_NAME ?? `database ${databaseId}`} on ${boundBackend}, and the session uses ${controlApiEndpoint}.`,
|
|
134
|
+
hint: 'Switch the session to that backend, or bind this directory to a database on this one (the person approves it).',
|
|
135
|
+
calls: [NEW_DATABASE],
|
|
136
|
+
});
|
|
137
|
+
}
|
|
116
138
|
const apiEndpoint = env.API_ENDPOINT || controlApiEndpoint;
|
|
117
139
|
const modulesEndpoint = env.MODULES_ENDPOINT || controlModulesEndpoint;
|
|
118
140
|
const controlHeaders = { Authorization: `Bearer ${accountBearer}` };
|
|
@@ -131,7 +153,7 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
131
153
|
}
|
|
132
154
|
if (probe.status === 'missing') {
|
|
133
155
|
return unresolved('db-missing', {
|
|
134
|
-
message: `The bound
|
|
156
|
+
message: `The database this project is bound to (ID ${databaseId}) no longer exists on this backend, or this account lost access to it.`,
|
|
135
157
|
hint: reprovisionAdvice(host),
|
|
136
158
|
...replaceCall(host),
|
|
137
159
|
});
|
|
@@ -148,7 +170,7 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
148
170
|
sessionUserId &&
|
|
149
171
|
probe.ownerId !== sessionUserId) {
|
|
150
172
|
return unresolved('account-mismatch', {
|
|
151
|
-
message: `The bound
|
|
173
|
+
message: `The database this project is bound to (ID ${databaseId}) was provisioned under a different account than the one signed in.`,
|
|
152
174
|
hint: reprovisionAdvice(host),
|
|
153
175
|
...replaceCall(host),
|
|
154
176
|
});
|
|
@@ -158,7 +180,7 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
158
180
|
if (probe.status === 'suspended') {
|
|
159
181
|
const why = probe.suspendedReason ? ` (${probe.suspendedReason})` : '';
|
|
160
182
|
return unresolved('db-suspended', {
|
|
161
|
-
message: `The bound
|
|
183
|
+
message: `The database this project is bound to (ID ${databaseId}) is suspended${why} since ${probe.suspendedAt}. Its data is kept, but nothing can read or change it until the suspension is lifted.`,
|
|
162
184
|
});
|
|
163
185
|
}
|
|
164
186
|
const databaseName = env.DATABASE_NAME || probe.name || '';
|
|
@@ -233,8 +255,7 @@ function appSignedOut(why = 'Not signed in to the app database yet.') {
|
|
|
233
255
|
const failure = {
|
|
234
256
|
code: 'app-signed-out',
|
|
235
257
|
message: why,
|
|
236
|
-
hint: getHost().
|
|
237
|
-
'Sign in from the Sheets tab, or open the Preview and sign in to your app, then try again.',
|
|
258
|
+
hint: getHost().appSignInHint ?? 'Sign in from the Sheets tab, or open the Preview and sign in to your app, then try again.',
|
|
238
259
|
};
|
|
239
260
|
return { reason: failureText(failure), failure };
|
|
240
261
|
}
|
|
@@ -249,10 +270,10 @@ export async function resolveDataToken(context) {
|
|
|
249
270
|
const broker = host.dataAuthBroker;
|
|
250
271
|
const active = broker?.getActiveToken(context.databaseId);
|
|
251
272
|
if (active)
|
|
252
|
-
return { token: active.token, userId: active.userId };
|
|
273
|
+
return { token: active.token, userId: active.userId, email: active.email };
|
|
253
274
|
let preview = null;
|
|
254
275
|
try {
|
|
255
|
-
preview = host.previewToken ? await host.previewToken() : null;
|
|
276
|
+
preview = host.previewToken ? await host.previewToken({ databaseId: context.databaseId, databaseName: context.databaseName }) : null;
|
|
256
277
|
}
|
|
257
278
|
catch (err) {
|
|
258
279
|
return appSignedOut(`Could not read the app sign-in: ${err instanceof Error ? err.message : String(err)}`);
|
|
@@ -268,11 +289,12 @@ export async function resolveDataToken(context) {
|
|
|
268
289
|
return appSignedOut();
|
|
269
290
|
broker?.adoptToken(context.databaseId, {
|
|
270
291
|
userId: preview.userId,
|
|
292
|
+
email: preview.email,
|
|
271
293
|
token: preview.accessToken,
|
|
272
294
|
expiresAt,
|
|
273
295
|
origin: 'harvest',
|
|
274
296
|
});
|
|
275
|
-
return { token: preview.accessToken, userId: preview.userId };
|
|
297
|
+
return { token: preview.accessToken, userId: preview.userId, email: preview.email };
|
|
276
298
|
}
|
|
277
299
|
// Per-DB endpoints are deterministic on the provisioning subdomain (= DATABASE_NAME):
|
|
278
300
|
// each plane swaps the first host label of its control-plane endpoint
|
|
@@ -293,18 +315,33 @@ export function deriveSubdomainEndpoint(baseEndpoint, firstLabel) {
|
|
|
293
315
|
// user the database's ownerId points at. Best-effort display lookup only —
|
|
294
316
|
// binding health never depends on it. Authenticates with the account bearer
|
|
295
317
|
// against the app-configured auth endpoint (the platform rejects per-database
|
|
296
|
-
// keys, and the bearer never goes to
|
|
318
|
+
// keys, and the bearer never goes to an injected pin).
|
|
319
|
+
// The bearer goes only to an auth plane the host names, or to the one an `api.` host implies (api.x -> auth.x):
|
|
320
|
+
// any other host would be a guess.
|
|
321
|
+
function derivedAuthEndpoint(apiEndpoint) {
|
|
322
|
+
if (!apiEndpoint)
|
|
323
|
+
return undefined;
|
|
324
|
+
try {
|
|
325
|
+
if (new URL(apiEndpoint).hostname.split('.')[0] !== 'api')
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
return undefined;
|
|
330
|
+
}
|
|
331
|
+
return deriveSubdomainEndpoint(apiEndpoint, 'auth') || undefined;
|
|
332
|
+
}
|
|
297
333
|
export async function resolveOrgName(ownerId) {
|
|
298
334
|
try {
|
|
299
335
|
const host = getHost();
|
|
300
|
-
const
|
|
336
|
+
const config = host.backendConfig();
|
|
337
|
+
const accountRead = await readAccount(host, config);
|
|
338
|
+
if (accountRead.kind === 'failed')
|
|
339
|
+
return undefined;
|
|
340
|
+
const account = accountRead.value;
|
|
301
341
|
const bearer = account?.apiKey ?? account?.accessToken;
|
|
302
342
|
if (!bearer)
|
|
303
343
|
return undefined;
|
|
304
|
-
const
|
|
305
|
-
if (!apiEndpoint)
|
|
306
|
-
return undefined;
|
|
307
|
-
const authEndpoint = deriveSubdomainEndpoint(apiEndpoint, 'auth');
|
|
344
|
+
const authEndpoint = config?.authEndpoint ?? derivedAuthEndpoint(config?.apiEndpoint);
|
|
308
345
|
if (!authEndpoint)
|
|
309
346
|
return undefined;
|
|
310
347
|
const client = auth.createClient({
|
package/esm/db-probe.js
CHANGED
|
@@ -19,7 +19,7 @@ const document = (databaseId, fields) => `
|
|
|
19
19
|
// rejected account credential, an error this probe doesn't know); recovery is
|
|
20
20
|
// a retry or a sign-in, never a reprovision.
|
|
21
21
|
//
|
|
22
|
-
// The probe authenticates with the ACCOUNT bearer, not
|
|
22
|
+
// The probe authenticates with the ACCOUNT bearer, not an injected project key:
|
|
23
23
|
// the platform api rejects per-database keys, and the singular `database(id:)`
|
|
24
24
|
// field is gone from the current contract, so the lookup goes through the
|
|
25
25
|
// `databases` collection filter.
|
|
@@ -97,7 +97,7 @@ export async function probeBoundDatabase(databaseId) {
|
|
|
97
97
|
if (backendRead.kind === 'failed') {
|
|
98
98
|
return { status: 'unreachable', databaseId, cause: 'error', detail: failureText(backendRead.failure) };
|
|
99
99
|
}
|
|
100
|
-
const accountRead = await readAccount(host);
|
|
100
|
+
const accountRead = await readAccount(host, backendRead.value);
|
|
101
101
|
if (accountRead.kind === 'failed') {
|
|
102
102
|
return { status: 'unreachable', databaseId, cause: 'auth', detail: failureText(accountRead.failure) };
|
|
103
103
|
}
|
package/esm/failure.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export declare function noRowCreated(action: string, check?: ToolCallSuggestion)
|
|
|
42
42
|
* A project binding with values that are not of their form (`keys`, in `where`): none of them is used, sent or shown,
|
|
43
43
|
* so the project acts as bound to no database until the person fixes them.
|
|
44
44
|
*/
|
|
45
|
-
export declare function invalidBinding(keys: readonly string[], where
|
|
45
|
+
export declare function invalidBinding(keys: readonly string[], where: string): ToolFailure;
|
|
46
46
|
export declare const MISSING_OWNER: ToolFailure;
|
|
47
47
|
/**
|
|
48
48
|
* What the call means, when it knows. `taken` names a unique value that is
|
package/esm/failure.js
CHANGED
|
@@ -65,7 +65,7 @@ export function noRowCreated(action, check) {
|
|
|
65
65
|
* A project binding with values that are not of their form (`keys`, in `where`): none of them is used, sent or shown,
|
|
66
66
|
* so the project acts as bound to no database until the person fixes them.
|
|
67
67
|
*/
|
|
68
|
-
export function invalidBinding(keys, where
|
|
68
|
+
export function invalidBinding(keys, where) {
|
|
69
69
|
return {
|
|
70
70
|
code: 'missing-credentials',
|
|
71
71
|
message: `The project's database entry is not valid: ${listed(keys.map((key) => `${key} in ${where}`))} ${keys.length === 1 ? 'does' : 'do'} not hold a value ` +
|
|
@@ -76,7 +76,7 @@ export function invalidBinding(keys, where = '.env') {
|
|
|
76
76
|
// provision_database fills a missing OWNER_ID when it keeps a live binding.
|
|
77
77
|
export const MISSING_OWNER = {
|
|
78
78
|
code: 'missing-credentials',
|
|
79
|
-
message: 'The project binding has no owner
|
|
79
|
+
message: 'The project binding has no owner, and the backend reported none.',
|
|
80
80
|
hint: "Bind the directory again under the project's database name to refresh the binding, then retry.",
|
|
81
81
|
calls: [suggest('provision_database', {}, "Refresh the binding: give the project's database name", ['database_name'])],
|
|
82
82
|
};
|
package/esm/gate.js
CHANGED
|
@@ -24,7 +24,7 @@ async function toolPrecondition(toolName, input, cwd) {
|
|
|
24
24
|
return blocked(invalidInput(parsed.error));
|
|
25
25
|
try {
|
|
26
26
|
const ready = await check(parsed.data, cwd);
|
|
27
|
-
return ready.kind === 'ok' ? { kind: 'ready', ...ready.value } : blocked(ready.failure);
|
|
27
|
+
return ready.kind === 'ok' ? { kind: 'ready', ...ready.value, input: parsed.data } : blocked(ready.failure);
|
|
28
28
|
}
|
|
29
29
|
catch (err) {
|
|
30
30
|
// A check that cannot finish blocks the call rather than let it through.
|
package/esm/graphql-http.d.ts
CHANGED
|
@@ -48,8 +48,9 @@ export declare const answersSignedOut: (err: unknown) => boolean;
|
|
|
48
48
|
export declare function classifyBackendErrors(errors: readonly GraphQLError[]): BackendErrorKind;
|
|
49
49
|
/**
|
|
50
50
|
* What a failed answer was, in fixed words: each error's public code, HTTP status or error id. The text a server writes
|
|
51
|
-
* is
|
|
52
|
-
*
|
|
51
|
+
* is left out, since a server can pass the database's own text through, with internal table names and error codes.
|
|
52
|
+
* Two texts are kept: a refused query's, which GraphQL writes about the query before it reads any data, and a request
|
|
53
|
+
* that got no answer, whose text postGraphQL made from the local fetch failure.
|
|
53
54
|
*/
|
|
54
55
|
export declare function answerText(errors: readonly GraphQLError[]): string;
|
|
55
56
|
/** A thrown error in words that hold no server text: an SDK error by its GraphQL errors, any other by its own message. */
|
package/esm/graphql-http.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// the body's errors keep their `extensions.code`, and every error carries the
|
|
6
6
|
// HTTP status it arrived with.
|
|
7
7
|
import { listed } from '@agentic-kit/harness';
|
|
8
|
+
import { format, getDefinition } from '@constructive-io/errors';
|
|
8
9
|
import { createFetch, } from '@constructive-io/graphql-query/runtime';
|
|
9
10
|
const errorText = (err) => (err instanceof Error ? err.message : String(err));
|
|
10
11
|
export async function postGraphQL(args) {
|
|
@@ -173,21 +174,39 @@ export function classifyBackendErrors(errors) {
|
|
|
173
174
|
// A code a server means for its caller is a word in capitals (FORBIDDEN, UNIQUE_VIOLATION). A database error's own
|
|
174
175
|
// code has a digit in its first word, so it is never named.
|
|
175
176
|
const PUBLIC_CODE = /^[A-Z]+(?:_[A-Z0-9]+)*$/;
|
|
176
|
-
//
|
|
177
|
+
// GraphQL refuses a query it cannot run before it reads any data, with a location in the query and no field path. Its
|
|
178
|
+
// words describe the caller's own query, so they are shown.
|
|
179
|
+
const queryLocation = (error) => {
|
|
180
|
+
const [first] = error.locations ?? [];
|
|
181
|
+
return first && !error.path && !error.extensions?.errorId && !codeOf(error) ? first : undefined;
|
|
182
|
+
};
|
|
183
|
+
// One error in fixed words: a logged error by its id, else the words of a refused query, else the words the backend's
|
|
184
|
+
// error registry (@constructive-io/errors) ships for a public code, written for the person who sees them, else its
|
|
185
|
+
// public code, else its HTTP status.
|
|
177
186
|
function errorWords(error) {
|
|
178
187
|
const id = error?.extensions?.errorId;
|
|
179
188
|
if (typeof id === 'string')
|
|
180
|
-
return `an internal error (error id ${id})`;
|
|
189
|
+
return `with an internal error (error id ${id})`;
|
|
190
|
+
const at = queryLocation(error);
|
|
191
|
+
if (at)
|
|
192
|
+
return `with the query error at line ${at.line}, column ${at.column} (${error.message})`;
|
|
181
193
|
const code = codeOf(error);
|
|
194
|
+
if (code && getDefinition(code)?.class === 'public')
|
|
195
|
+
return `"${format(code, contextOf(error)).replace(/\.$/, '')}"`;
|
|
182
196
|
if (code && PUBLIC_CODE.test(code))
|
|
183
|
-
return `the code ${code}`;
|
|
197
|
+
return `with the code ${code}`;
|
|
184
198
|
const status = statusOf(error);
|
|
185
|
-
return status === undefined || status === 200 ? 'an error without a public code' : `HTTP ${status}`;
|
|
199
|
+
return status === undefined || status === 200 ? 'with an error without a public code' : `with HTTP ${status}`;
|
|
186
200
|
}
|
|
201
|
+
const contextOf = (error) => {
|
|
202
|
+
const context = error?.extensions?.context;
|
|
203
|
+
return context && typeof context === 'object' ? context : {};
|
|
204
|
+
};
|
|
187
205
|
/**
|
|
188
206
|
* What a failed answer was, in fixed words: each error's public code, HTTP status or error id. The text a server writes
|
|
189
|
-
* is
|
|
190
|
-
*
|
|
207
|
+
* is left out, since a server can pass the database's own text through, with internal table names and error codes.
|
|
208
|
+
* Two texts are kept: a refused query's, which GraphQL writes about the query before it reads any data, and a request
|
|
209
|
+
* that got no answer, whose text postGraphQL made from the local fetch failure.
|
|
191
210
|
*/
|
|
192
211
|
export function answerText(errors) {
|
|
193
212
|
const list = errors ?? [];
|
|
@@ -195,7 +214,7 @@ export function answerText(errors) {
|
|
|
195
214
|
if (unanswered)
|
|
196
215
|
return `the backend did not answer (${unanswered.message || 'no response'})`;
|
|
197
216
|
const words = [...new Set(list.map(errorWords))];
|
|
198
|
-
return `the backend answered
|
|
217
|
+
return `the backend answered ${words.length ? listed(words) : 'with an error without a public code'}`;
|
|
199
218
|
}
|
|
200
219
|
/** A thrown error in words that hold no server text: an SDK error by its GraphQL errors, any other by its own message. */
|
|
201
220
|
export function thrownText(err) {
|
package/esm/host.d.ts
CHANGED
|
@@ -20,9 +20,12 @@ export type HostAccount = {
|
|
|
20
20
|
agent?: PlanAgent;
|
|
21
21
|
accessToken: string;
|
|
22
22
|
apiKey?: string;
|
|
23
|
+
backend?: HostBackendConfig;
|
|
23
24
|
};
|
|
24
25
|
export type HostBackendConfig = {
|
|
25
26
|
apiEndpoint?: string;
|
|
27
|
+
/** The auth plane, when the host knows it; without it, it is derived only from an `api.` host. */
|
|
28
|
+
authEndpoint?: string;
|
|
26
29
|
modulesEndpoint?: string;
|
|
27
30
|
agentEndpoint?: string;
|
|
28
31
|
computeEndpoint?: string;
|
|
@@ -35,6 +38,7 @@ export declare function noBackendReason(hint?: string): string;
|
|
|
35
38
|
export type ActiveDataToken = {
|
|
36
39
|
token: string;
|
|
37
40
|
userId?: string;
|
|
41
|
+
email?: string;
|
|
38
42
|
expiresAt: number;
|
|
39
43
|
origin?: string;
|
|
40
44
|
};
|
|
@@ -53,6 +57,7 @@ export type PreviewToken = {
|
|
|
53
57
|
accessToken: string;
|
|
54
58
|
accessTokenExpiresAt?: string;
|
|
55
59
|
userId?: string;
|
|
60
|
+
email?: string;
|
|
56
61
|
};
|
|
57
62
|
/**
|
|
58
63
|
* Overlay layered over the pinned base preset when provisioning a database.
|
|
@@ -89,8 +94,8 @@ export type StepUpRequest = {
|
|
|
89
94
|
};
|
|
90
95
|
/**
|
|
91
96
|
* The database a project is bound to, handed to the host before provisioning
|
|
92
|
-
* writes it to the project's `.
|
|
93
|
-
* the binding never ends up with a
|
|
97
|
+
* writes it to the project's `.constructive/project.json`, so a host that keeps
|
|
98
|
+
* its own record of the binding never ends up with a binding it doesn't know about.
|
|
94
99
|
*/
|
|
95
100
|
export type ProjectBinding = {
|
|
96
101
|
cwd: string;
|
|
@@ -120,8 +125,16 @@ export interface ToolsHost {
|
|
|
120
125
|
* the desktop wording.
|
|
121
126
|
*/
|
|
122
127
|
signInHint?: string;
|
|
123
|
-
/**
|
|
124
|
-
|
|
128
|
+
/**
|
|
129
|
+
* The token of a user of the app of `database`, signed in to the app's own auth plane: harvested from the host's
|
|
130
|
+
* app preview, or one the host keeps for that database. Null when no one is signed in to that app.
|
|
131
|
+
*/
|
|
132
|
+
previewToken?(database: {
|
|
133
|
+
databaseId: string;
|
|
134
|
+
databaseName: string;
|
|
135
|
+
}): Promise<PreviewToken | null>;
|
|
136
|
+
/** Host-specific instruction for signing in to the app, in place of the desktop wording, when a record tool finds no one. */
|
|
137
|
+
appSignInHint?: string;
|
|
125
138
|
/** Treat tokens expiring within this window as already expired. Default 30s. */
|
|
126
139
|
dataTokenSkewMs?: number;
|
|
127
140
|
/**
|
|
@@ -172,8 +185,8 @@ export interface ToolsHost {
|
|
|
172
185
|
prewarmApp?: boolean;
|
|
173
186
|
/**
|
|
174
187
|
* Save a binding in the host's own store. Provisioning calls it before
|
|
175
|
-
* writing
|
|
176
|
-
* throws,
|
|
188
|
+
* writing the project file, on a new binding and on a kept one (`skipped`). If it
|
|
189
|
+
* throws, the binding is left alone, the provision ticket stays recorded, and
|
|
177
190
|
* the call fails with `binding-not-saved`.
|
|
178
191
|
*/
|
|
179
192
|
persistBinding?(binding: ProjectBinding): Promise<void>;
|
|
@@ -196,5 +209,5 @@ export declare function configureHost(host: ToolsHost): void;
|
|
|
196
209
|
/** The host's backend config, or `no-backend` when the host could not give it. */
|
|
197
210
|
export declare function readBackendConfig(host: ToolsHost): Result<HostBackendConfig | null | undefined>;
|
|
198
211
|
/** The signed-in account, or `signed-out` when the host could not give it (it threw, or its promise rejected). */
|
|
199
|
-
export declare function readAccount(host: ToolsHost): Promise<Result<HostAccount | null | undefined>>;
|
|
212
|
+
export declare function readAccount(host: ToolsHost, backend?: HostBackendConfig | null): Promise<Result<HostAccount | null | undefined>>;
|
|
200
213
|
export declare function getHost(): ToolsHost;
|
package/esm/host.js
CHANGED
|
@@ -56,9 +56,14 @@ export function readBackendConfig(host) {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
/** The signed-in account, or `signed-out` when the host could not give it (it threw, or its promise rejected). */
|
|
59
|
-
export async function readAccount(host) {
|
|
59
|
+
export async function readAccount(host, backend) {
|
|
60
60
|
try {
|
|
61
|
-
|
|
61
|
+
const account = await host.account();
|
|
62
|
+
if (account?.backend && (!backend || Object.keys(account.backend)
|
|
63
|
+
.some((plane) => account.backend[plane] !== backend[plane]))) {
|
|
64
|
+
return { kind: 'failed', failure: { code: 'signed-out', message: 'The account and backend no longer match. Retry the command.' } };
|
|
65
|
+
}
|
|
66
|
+
return { kind: 'ok', value: account };
|
|
62
67
|
}
|
|
63
68
|
catch (err) {
|
|
64
69
|
return {
|