@agentic-kit/db-tools 0.12.47 → 0.13.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 +18 -4
- package/access/findings.d.ts +93 -0
- package/access/findings.js +434 -0
- package/access/index.d.ts +5 -0
- package/access/index.js +27 -0
- package/access/policies.d.ts +142 -0
- package/access/policies.js +338 -0
- package/access/provisioning.d.ts +121 -0
- package/access/provisioning.js +381 -0
- package/access/rule-check.d.ts +52 -0
- package/access/rule-check.js +307 -0
- package/access/words.d.ts +38 -0
- package/access/words.js +530 -0
- package/app-workspace.d.ts +1 -0
- package/app-workspace.js +7 -9
- package/blueprint/blueprint-defaults.d.ts +9 -0
- package/blueprint/blueprint-defaults.js +77 -0
- package/blueprint/blueprint-schema.d.ts +68 -0
- package/blueprint/blueprint-schema.js +139 -0
- package/blueprint/field-type.d.ts +4 -0
- package/blueprint/field-type.js +100 -0
- package/blueprint/internal-policies.d.ts +1 -0
- package/blueprint/internal-policies.js +10 -0
- package/blueprint-construction.d.ts +10 -6
- package/blueprint-construction.js +34 -25
- package/context.d.ts +15 -0
- package/context.js +132 -69
- package/db-probe.d.ts +1 -1
- package/db-probe.js +19 -4
- package/esm/access/findings.d.ts +93 -0
- package/esm/access/findings.js +428 -0
- package/esm/access/index.d.ts +5 -0
- package/esm/access/index.js +5 -0
- package/esm/access/policies.d.ts +142 -0
- package/esm/access/policies.js +315 -0
- package/esm/access/provisioning.d.ts +121 -0
- package/esm/access/provisioning.js +375 -0
- package/esm/access/rule-check.d.ts +52 -0
- package/esm/access/rule-check.js +297 -0
- package/esm/access/words.d.ts +38 -0
- package/esm/access/words.js +524 -0
- package/esm/app-workspace.d.ts +1 -0
- package/esm/app-workspace.js +7 -10
- package/esm/blueprint/blueprint-defaults.d.ts +9 -0
- package/esm/blueprint/blueprint-defaults.js +74 -0
- package/esm/blueprint/blueprint-schema.d.ts +68 -0
- package/esm/blueprint/blueprint-schema.js +134 -0
- package/esm/blueprint/field-type.d.ts +4 -0
- package/esm/blueprint/field-type.js +95 -0
- package/esm/blueprint/internal-policies.d.ts +1 -0
- package/esm/blueprint/internal-policies.js +7 -0
- package/esm/blueprint-construction.d.ts +10 -6
- package/esm/blueprint-construction.js +32 -24
- package/esm/context.d.ts +15 -0
- package/esm/context.js +133 -70
- package/esm/db-probe.d.ts +1 -1
- package/esm/db-probe.js +21 -6
- package/esm/failure.d.ts +89 -0
- package/esm/failure.js +362 -0
- package/esm/gate.d.ts +6 -4
- package/esm/gate.js +42 -14
- package/esm/graphql-http.d.ts +27 -6
- package/esm/graphql-http.js +96 -24
- package/esm/host.d.ts +33 -8
- package/esm/host.js +42 -2
- package/esm/index.d.ts +22 -12
- package/esm/index.js +21 -39
- package/esm/provision-database/env-file.d.ts +7 -0
- package/esm/provision-database/env-file.js +30 -0
- package/esm/provision-database/preset-match.js +1 -2
- package/esm/provision-database/provision.d.ts +21 -4
- package/esm/provision-database/provision.js +330 -85
- package/esm/provision-database/request-database.d.ts +22 -3
- package/esm/provision-database/request-database.js +46 -13
- package/esm/ready.d.ts +20 -0
- package/esm/ready.js +45 -0
- package/esm/run-codegen/barrels.js +1 -1
- package/esm/run-codegen/endpoints.js +5 -6
- package/esm/schema-model/normalize.js +1 -1
- package/esm/schema-model.js +4 -5
- package/esm/schema-resolve.d.ts +50 -0
- package/esm/schema-resolve.js +200 -14
- package/esm/table-access.d.ts +116 -0
- package/esm/table-access.js +418 -0
- package/esm/tools/access.d.ts +218 -0
- package/esm/tools/access.js +651 -0
- package/esm/tools/add-policies.d.ts +44 -12
- package/esm/tools/add-policies.js +84 -84
- package/esm/tools/add-records.d.ts +23 -2
- package/esm/tools/add-records.js +113 -75
- package/esm/tools/add-relation-schema.d.ts +5 -5
- package/esm/tools/add-relation-schema.js +13 -14
- package/esm/tools/add-relation.d.ts +11 -2
- package/esm/tools/add-relation.js +68 -31
- package/esm/tools/create-api-key.d.ts +18 -4
- package/esm/tools/create-api-key.js +105 -54
- package/esm/tools/describe-schema.d.ts +5 -7
- package/esm/tools/describe-schema.js +41 -118
- package/esm/tools/entity-types.d.ts +54 -0
- package/esm/tools/entity-types.js +233 -0
- package/esm/tools/index.d.ts +26 -0
- package/esm/tools/index.js +151 -0
- package/esm/tools/mutations.d.ts +36 -7
- package/esm/tools/mutations.js +245 -99
- package/esm/tools/provision-blueprint.d.ts +25 -1
- package/esm/tools/provision-blueprint.js +184 -42
- package/esm/tools/provision-database.d.ts +7 -5
- package/esm/tools/provision-database.js +31 -17
- package/esm/tools/run-codegen.d.ts +27 -4
- package/esm/tools/run-codegen.js +143 -76
- package/esm/tools/templates.d.ts +30 -7
- package/esm/tools/templates.js +166 -95
- package/failure.d.ts +89 -0
- package/failure.js +375 -0
- package/gate.d.ts +6 -4
- package/gate.js +41 -13
- package/graphql-http.d.ts +27 -6
- package/graphql-http.js +105 -25
- package/host.d.ts +33 -8
- package/host.js +45 -2
- package/index.d.ts +22 -12
- package/index.js +86 -43
- package/package.json +6 -5
- package/provision-database/env-file.d.ts +7 -0
- package/provision-database/env-file.js +32 -1
- package/provision-database/preset-match.js +1 -2
- package/provision-database/provision.d.ts +21 -4
- package/provision-database/provision.js +327 -81
- package/provision-database/request-database.d.ts +22 -3
- package/provision-database/request-database.js +49 -14
- package/ready.d.ts +20 -0
- package/ready.js +52 -0
- package/run-codegen/barrels.js +1 -1
- package/run-codegen/endpoints.js +5 -6
- package/schema-model/normalize.js +4 -4
- package/schema-model.js +4 -5
- package/schema-resolve.d.ts +50 -0
- package/schema-resolve.js +209 -14
- package/table-access.d.ts +116 -0
- package/table-access.js +437 -0
- package/tools/access.d.ts +218 -0
- package/tools/access.js +669 -0
- package/tools/add-policies.d.ts +44 -12
- package/tools/add-policies.js +86 -84
- package/tools/add-records.d.ts +23 -2
- package/tools/add-records.js +113 -74
- package/tools/add-relation-schema.d.ts +5 -5
- package/tools/add-relation-schema.js +13 -14
- package/tools/add-relation.d.ts +11 -2
- package/tools/add-relation.js +69 -31
- package/tools/create-api-key.d.ts +18 -4
- package/tools/create-api-key.js +104 -53
- package/tools/describe-schema.d.ts +5 -7
- package/tools/describe-schema.js +40 -117
- package/tools/entity-types.d.ts +54 -0
- package/tools/entity-types.js +240 -0
- package/tools/index.d.ts +26 -0
- package/tools/index.js +154 -0
- package/tools/mutations.d.ts +36 -7
- package/tools/mutations.js +252 -101
- package/tools/provision-blueprint.d.ts +25 -1
- package/tools/provision-blueprint.js +186 -43
- package/tools/provision-database.d.ts +7 -5
- package/tools/provision-database.js +33 -18
- package/tools/run-codegen.d.ts +27 -4
- package/tools/run-codegen.js +141 -73
- package/tools/templates.d.ts +30 -7
- package/tools/templates.js +167 -94
- package/esm/policy/add-policies-to-table.d.ts +0 -26
- package/esm/policy/add-policies-to-table.js +0 -66
- package/esm/policy/provision-helpers.d.ts +0 -38
- package/esm/policy/provision-helpers.js +0 -50
- package/esm/tools/manage-entity-types.d.ts +0 -40
- package/esm/tools/manage-entity-types.js +0 -201
- package/policy/add-policies-to-table.d.ts +0 -26
- package/policy/add-policies-to-table.js +0 -71
- package/policy/provision-helpers.d.ts +0 -38
- package/policy/provision-helpers.js +0 -58
- package/tools/manage-entity-types.d.ts +0 -40
- package/tools/manage-entity-types.js +0 -206
|
@@ -1,12 +1,160 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { dataNodesForPolicies, fieldsAddedByNodes, ROW_ID, tablePolicyFromBlueprint } from '../access';
|
|
2
|
+
import { columnsOf } from '../access/words';
|
|
3
|
+
import { expandBlueprintDefaults } from '../blueprint/blueprint-defaults';
|
|
4
|
+
import { blueprintProblems, BlueprintZod } from '../blueprint/blueprint-schema';
|
|
5
|
+
import { fieldTypeToTypeName } from '../blueprint/field-type';
|
|
2
6
|
import { BlueprintBuildError, buildBlueprint } from '../blueprint-construction';
|
|
3
|
-
import {
|
|
7
|
+
import { DESCRIBE_SCHEMA, failedResult, failureFromError, noRowCreated } from '../failure';
|
|
8
|
+
import { ownedProjectReady } from '../ready';
|
|
9
|
+
import { readSchemas, relationTablesMissing, schemasMissing, storedName, storedTableNames, tablesTaken } from '../schema-resolve';
|
|
10
|
+
import { columnChange } from '../table-access';
|
|
11
|
+
import { relatedColumns, rolesFor, ruleProblems, rulesToSend } from './access';
|
|
12
|
+
// The rules of a table entry that are rule objects; anything else in the list is the schema's to refuse.
|
|
13
|
+
const rulesOf = (table) => (Array.isArray(table.policies) ? table.policies : []).filter((policy) => typeof policy === 'object' && policy !== null && typeof policy.$type === 'string');
|
|
14
|
+
// The new tables of a blueprint: the entries that name a table (an entry may attach to a module's table instead).
|
|
15
|
+
const newTables = (definition) => definition.tables.filter((table) => typeof table.table_name === 'string');
|
|
16
|
+
// The columns of each new table, by its name: its own fields, the row id, and the columns its nodes add.
|
|
17
|
+
function newTableColumns(definition) {
|
|
18
|
+
return Object.fromEntries(newTables(definition).map((table) => [
|
|
19
|
+
table.table_name,
|
|
20
|
+
[...new Set([ROW_ID, ...(table.fields ?? []).map((field) => field.name), ...fieldsAddedByNodes(table.nodes ?? []).fields.map((column) => column.name)])],
|
|
21
|
+
]));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A field of a new table that a node of the table also adds, when the backend cannot keep it: the row id, which the
|
|
25
|
+
* table's own id node makes, or a column of another type than the node's. The backend creates a field before the
|
|
26
|
+
* nodes, and a node keeps a column that exists as it is.
|
|
27
|
+
*/
|
|
28
|
+
function fieldNodeClash(table) {
|
|
29
|
+
const added = fieldsAddedByNodes(table.nodes ?? []).fields;
|
|
30
|
+
for (const field of table.fields ?? []) {
|
|
31
|
+
const column = added.find((entry) => entry.name === field.name);
|
|
32
|
+
if (field.name === ROW_ID) {
|
|
33
|
+
return {
|
|
34
|
+
code: 'invalid-input',
|
|
35
|
+
message: `Table "${table.table_name}": the field ${ROW_ID} is the row id, which every table gets. Leave it out of the fields.`,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const type = fieldTypeToTypeName(field.type);
|
|
39
|
+
if (column && column.type !== type) {
|
|
40
|
+
return {
|
|
41
|
+
code: 'invalid-input',
|
|
42
|
+
message: `Table "${table.table_name}": the field ${field.name} is ${type}, and ${column.node} adds ${field.name} as ${column.type} for the table's rules or nodes; ` +
|
|
43
|
+
'the backend keeps the field as given, so they cannot work on it.',
|
|
44
|
+
hint: `Leave ${field.name} out of the fields so ${column.node} adds it, or give it the type ${column.type}.`,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The checks of a blueprint that need no backend: each rule's shape and data against its node (the columns of the
|
|
52
|
+
* other tables it reads included), the rules each table sends (`rulesToSend`), the registry's blueprint JSON Schema
|
|
53
|
+
* over what the backend would receive, and for each new table its fields beside its nodes, the columns its rules read
|
|
54
|
+
* and the Data nodes that add them. It answers the definition with each table's rules as the backend creates them.
|
|
55
|
+
*/
|
|
56
|
+
function blueprintChecks(definition, context) {
|
|
57
|
+
const tables = [];
|
|
58
|
+
for (const table of definition.tables) {
|
|
59
|
+
const name = table.table_name ?? 'of the module';
|
|
60
|
+
const policies = rulesOf(table);
|
|
61
|
+
// A rule list that is not a list of rules is the schema's to say.
|
|
62
|
+
if (policies.length !== (Array.isArray(table.policies) ? table.policies.length : 0)) {
|
|
63
|
+
tables.push(table);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const label = (index) => `Table "${name}", rule ${index + 1} (${policies[index].$type})`;
|
|
67
|
+
const rules = ruleProblems(policies, { roles: context.roles, inBlueprint: true, withCheck: true, columnsOf: columnsOf(context.related) }, label);
|
|
68
|
+
if (rules)
|
|
69
|
+
return { kind: 'failed', failure: rules };
|
|
70
|
+
const sent = rulesToSend(policies, { defaultRole: 'authenticated', label });
|
|
71
|
+
if (sent.kind === 'failed')
|
|
72
|
+
return sent;
|
|
73
|
+
tables.push(Array.isArray(table.policies) ? { ...table, policies: sent.value } : table);
|
|
74
|
+
}
|
|
75
|
+
const checked = { ...definition, tables };
|
|
76
|
+
const problems = blueprintProblems(checked);
|
|
77
|
+
if (problems.length) {
|
|
78
|
+
return {
|
|
79
|
+
kind: 'failed',
|
|
80
|
+
failure: {
|
|
81
|
+
code: 'invalid-input',
|
|
82
|
+
message: `The blueprint is not valid: ${problems.join(' ')}`,
|
|
83
|
+
hint: 'Fix each place the message names; the blueprint JSON Schema of node-type-registry describes every key.',
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
for (const table of newTables(checked)) {
|
|
88
|
+
const clash = fieldNodeClash(table);
|
|
89
|
+
if (clash)
|
|
90
|
+
return { kind: 'failed', failure: clash };
|
|
91
|
+
const rules = (table.policies ?? []).map(tablePolicyFromBlueprint);
|
|
92
|
+
const needed = dataNodesForPolicies(rules.flatMap((rule) => rule.with_check ? [rule, { type: rule.with_check.type, data: rule.with_check.data ?? {} }] : [rule]), new Set((table.fields ?? []).map(({ name }) => name)));
|
|
93
|
+
if (needed.problems.length)
|
|
94
|
+
return { kind: 'failed', failure: { code: 'invalid-input', message: `Table "${table.table_name}": ${needed.problems.join(' ')}` } };
|
|
95
|
+
const change = columnChange(table.table_name, (table.fields ?? []).map(({ name }) => ({ name })), table.nodes ?? [], rules);
|
|
96
|
+
if (change.kind === 'failed')
|
|
97
|
+
return { kind: 'failed', failure: { ...change.failure, message: `Table "${table.table_name}": ${change.failure.message}` } };
|
|
98
|
+
}
|
|
99
|
+
return { kind: 'ok', value: checked };
|
|
100
|
+
}
|
|
101
|
+
// Each schema the blueprint names: a table's own, an end of a relation, and the table of a top-level index or constraint.
|
|
102
|
+
function schemasNamed(definition) {
|
|
103
|
+
const items = [definition.indexes, definition.unique_constraints, definition.check_constraints, definition.full_text_searches].flatMap((list) => (list ?? []));
|
|
104
|
+
return [
|
|
105
|
+
...definition.tables.map((table) => table.schema_name),
|
|
106
|
+
...(definition.relations ?? []).flatMap((relation) => [relation.source_schema, relation.target_schema]),
|
|
107
|
+
...items.map((item) => item.schema_name),
|
|
108
|
+
].filter((name) => typeof name === 'string');
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* What provision_blueprint checks before it changes anything: an owned
|
|
112
|
+
* project, a blueprint the registry's JSON Schema takes once its defaults are
|
|
113
|
+
* filled in, rules that fit their nodes and the roles of the database, the
|
|
114
|
+
* schemas it names, none of its new tables yet, and the tables its relations
|
|
115
|
+
* name.
|
|
116
|
+
*/
|
|
117
|
+
export async function provisionBlueprintReady(params, cwd) {
|
|
118
|
+
const ready = await ownedProjectReady(cwd);
|
|
119
|
+
if (ready.kind === 'failed')
|
|
120
|
+
return ready;
|
|
121
|
+
const expanded = expandBlueprintDefaults(params.definition);
|
|
122
|
+
const rules = expanded.tables.flatMap(rulesOf);
|
|
123
|
+
const roles = await rolesFor(ready.value, rules);
|
|
124
|
+
if (roles.kind === 'failed')
|
|
125
|
+
return roles;
|
|
126
|
+
const existing = await relatedColumns(ready.value, rules);
|
|
127
|
+
if (existing.kind === 'failed')
|
|
128
|
+
return existing;
|
|
129
|
+
const related = existing.value ? { ...existing.value, ...newTableColumns(expanded) } : undefined;
|
|
130
|
+
const checked = blueprintChecks(expanded, { roles: roles.value, related });
|
|
131
|
+
if (checked.kind === 'failed')
|
|
132
|
+
return checked;
|
|
133
|
+
const definition = checked.value;
|
|
134
|
+
const named = schemasNamed(definition);
|
|
135
|
+
const schemas = named.length ? await readSchemas(ready.value) : { kind: 'ok', value: new Map() };
|
|
136
|
+
if (schemas.kind === 'failed')
|
|
137
|
+
return schemas;
|
|
138
|
+
const missingSchema = named.length ? schemasMissing(named, schemas.value) : null;
|
|
139
|
+
if (missingSchema)
|
|
140
|
+
return { kind: 'failed', failure: missingSchema };
|
|
141
|
+
const made = newTables(definition);
|
|
142
|
+
const taken = await tablesTaken(ready.value, made.map((table) => ({ name: table.table_name, ...(table.schema_name ? { schema: table.schema_name } : {}) })), schemas.value);
|
|
143
|
+
if (taken)
|
|
144
|
+
return { kind: 'failed', failure: taken };
|
|
145
|
+
const missing = await relationTablesMissing(ready.value, made.map((table) => table.table_name), (definition.relations ?? []), schemas.value);
|
|
146
|
+
if (missing)
|
|
147
|
+
return { kind: 'failed', failure: missing };
|
|
148
|
+
return { kind: 'ok', value: { ...ready.value, definition, ...(related ? { related } : {}) } };
|
|
149
|
+
}
|
|
4
150
|
function formatText(name, details) {
|
|
5
|
-
if (details.error)
|
|
6
|
-
return `Failed to provision "${name}": ${details.error}`;
|
|
7
151
|
const lines = [`Created ${details.created} table${details.created === 1 ? '' : 's'} from "${name}":`];
|
|
8
152
|
for (const table of details.tables) {
|
|
9
|
-
const parts = [
|
|
153
|
+
const parts = [
|
|
154
|
+
...(table.requestedName ? [`stored from "${table.requestedName}"`] : []),
|
|
155
|
+
`${table.fields.length} field${table.fields.length === 1 ? '' : 's'}`,
|
|
156
|
+
...table.fields.filter((field) => field.requestedName).map((field) => `field ${field.name} stored from "${field.requestedName}"`),
|
|
157
|
+
];
|
|
10
158
|
if (table.relationCount > 0) {
|
|
11
159
|
parts.push(`${table.relationCount} relation${table.relationCount === 1 ? '' : 's'}`);
|
|
12
160
|
}
|
|
@@ -17,13 +165,17 @@ function formatText(name, details) {
|
|
|
17
165
|
}
|
|
18
166
|
return lines.join('\n');
|
|
19
167
|
}
|
|
168
|
+
const typeText = (type) => (typeof type === 'string' ? type : JSON.stringify(type));
|
|
169
|
+
const defaultText = (value) => (value == null ? null : typeof value === 'string' ? value : JSON.stringify(value));
|
|
20
170
|
export const provisionBlueprintTool = {
|
|
21
171
|
name: 'provision_blueprint',
|
|
22
172
|
label: 'Provision blueprint',
|
|
23
|
-
description: 'Create one or more related tables (with fields,
|
|
24
|
-
promptSnippet: 'provision_blueprint: create new tables from scratch in a single call (fields
|
|
173
|
+
description: 'Create one or more related tables (with fields, access rules, relations, indexes and constraints) in the project database from a blueprint definition. Always put related tables in a SINGLE call so relations are created together.',
|
|
174
|
+
promptSnippet: 'provision_blueprint: create new tables from scratch in a single call (fields, rules, relations, indexes). Read the schema first. Gated.',
|
|
25
175
|
parameters: BlueprintZod,
|
|
26
|
-
|
|
176
|
+
effect: 'write',
|
|
177
|
+
async execute(input, ctx) {
|
|
178
|
+
const params = input;
|
|
27
179
|
const empty = {
|
|
28
180
|
created: 0,
|
|
29
181
|
total: 0,
|
|
@@ -31,19 +183,12 @@ export const provisionBlueprintTool = {
|
|
|
31
183
|
error: null,
|
|
32
184
|
constructionId: null,
|
|
33
185
|
};
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
const { modules, databaseId, schemaId, ownerId } = resolved.context;
|
|
39
|
-
if (!ownerId) {
|
|
40
|
-
return {
|
|
41
|
-
content: [{ type: 'text', text: 'Not authenticated (missing OWNER_ID in .env).' }],
|
|
42
|
-
details: empty,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
const definition = expandBlueprintDefaults(params.definition);
|
|
186
|
+
const ready = await provisionBlueprintReady(params, ctx.cwd);
|
|
187
|
+
if (ready.kind === 'failed')
|
|
188
|
+
return failedResult(ready.failure, empty);
|
|
189
|
+
const { modules, databaseId, schemaId, ownerId, definition } = ready.value;
|
|
46
190
|
const uniqueName = `${params.name}_${Date.now()}`;
|
|
191
|
+
const made = newTables(definition);
|
|
47
192
|
try {
|
|
48
193
|
const createResult = await modules.blueprint
|
|
49
194
|
.create({
|
|
@@ -53,29 +198,27 @@ export const provisionBlueprintTool = {
|
|
|
53
198
|
name: uniqueName,
|
|
54
199
|
displayName: params.name,
|
|
55
200
|
description: params.description,
|
|
56
|
-
definition,
|
|
201
|
+
definition: definition,
|
|
57
202
|
},
|
|
58
203
|
select: { id: true, name: true },
|
|
59
204
|
})
|
|
60
205
|
.unwrap();
|
|
61
206
|
const blueprintId = createResult.createBlueprint?.blueprint?.id;
|
|
62
207
|
if (!blueprintId)
|
|
63
|
-
|
|
64
|
-
const { constructionId } = await buildBlueprint(modules, { blueprintId, schemaId }, { signal: ctx.signal });
|
|
65
|
-
|
|
208
|
+
return failedResult(noRowCreated(`Failed to create the blueprint "${params.name}"`, DESCRIBE_SCHEMA), empty);
|
|
209
|
+
const { constructionId, tables } = await buildBlueprint(modules, { blueprintId, schemaId }, { signal: ctx.signal });
|
|
210
|
+
// The construction maps each table name of the blueprint to its id; the stored name is read back by id.
|
|
211
|
+
const stored = await storedTableNames(ready.value, Object.values(tables)).catch(() => new Map());
|
|
212
|
+
const named = (given, as, rest) => as && as !== given ? { name: as, requestedName: given, ...rest } : { name: given, ...rest };
|
|
213
|
+
const relations = (definition.relations ?? []);
|
|
214
|
+
const given = new Map(params.definition.tables.filter((table) => typeof table.table_name === 'string').map((table) => [table.table_name, table]));
|
|
66
215
|
const details = {
|
|
67
|
-
created:
|
|
68
|
-
total:
|
|
69
|
-
tables:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
type: f.type,
|
|
74
|
-
isRequired: f.is_required ?? false,
|
|
75
|
-
defaultValue: f.default ?? null,
|
|
76
|
-
})),
|
|
77
|
-
policies: filterInternalPolicies(t.policies.map((p) => p.$type)),
|
|
78
|
-
relationCount: relations.filter((r) => r.source_table === t.table_name).length,
|
|
216
|
+
created: made.length,
|
|
217
|
+
total: made.length,
|
|
218
|
+
tables: made.map((table) => named(table.table_name, stored.get(tables[table.table_name]) ?? storedName(table.table_name), {
|
|
219
|
+
fields: (given.get(table.table_name)?.fields ?? []).map((field) => named(field.name, storedName(field.name), { type: typeText(field.type), isRequired: field.is_required ?? false, defaultValue: defaultText(field.default) })),
|
|
220
|
+
policies: [...new Set((table.policies ?? []).map((policy) => policy.$type))],
|
|
221
|
+
relationCount: relations.filter((relation) => relation.source_table === table.table_name).length,
|
|
79
222
|
})),
|
|
80
223
|
error: null,
|
|
81
224
|
constructionId,
|
|
@@ -83,15 +226,14 @@ export const provisionBlueprintTool = {
|
|
|
83
226
|
return { content: [{ type: 'text', text: formatText(params.name, details) }], details };
|
|
84
227
|
}
|
|
85
228
|
catch (err) {
|
|
86
|
-
const
|
|
87
|
-
|
|
229
|
+
const failure = failureFromError(err, `Failed to provision "${params.name}"`);
|
|
230
|
+
return failedResult(failure, {
|
|
88
231
|
created: 0,
|
|
89
|
-
total:
|
|
232
|
+
total: made.length,
|
|
90
233
|
tables: [],
|
|
91
|
-
error: message,
|
|
234
|
+
error: failure.message,
|
|
92
235
|
constructionId: err instanceof BlueprintBuildError ? err.constructionId : null,
|
|
93
|
-
};
|
|
94
|
-
return { content: [{ type: 'text', text: formatText(params.name, details) }], details };
|
|
236
|
+
});
|
|
95
237
|
}
|
|
96
238
|
},
|
|
97
239
|
};
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type HarnessTool } from '@agentic-kit/harness';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { type RunCommand } from '../app-workspace';
|
|
4
4
|
import { type ProvisionDatabaseDetails } from '../provision-database/provision';
|
|
5
5
|
export type { ProvisionDatabaseDetails };
|
|
6
6
|
declare const ProvisionDatabaseZod: z.ZodObject<{
|
|
7
7
|
database_name: z.ZodString;
|
|
8
|
-
|
|
9
|
-
}, z.core.$strip>;
|
|
8
|
+
}, z.core.$strict>;
|
|
10
9
|
export declare function createProvisionDatabaseTool(exec?: RunCommand): HarnessTool<typeof ProvisionDatabaseZod, ProvisionDatabaseDetails>;
|
|
10
|
+
export declare function createReplaceDatabaseTool(exec?: RunCommand): HarnessTool<typeof ProvisionDatabaseZod, ProvisionDatabaseDetails>;
|
|
11
11
|
export declare const provisionDatabaseTool: HarnessTool<z.ZodObject<{
|
|
12
12
|
database_name: z.ZodString;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
}, z.core.$strict>, ProvisionDatabaseDetails>;
|
|
14
|
+
export declare const replaceDatabaseTool: HarnessTool<z.ZodObject<{
|
|
15
|
+
database_name: z.ZodString;
|
|
16
|
+
}, z.core.$strict>, ProvisionDatabaseDetails>;
|
|
@@ -1,34 +1,48 @@
|
|
|
1
|
+
import { strictObject } from '@agentic-kit/harness';
|
|
1
2
|
import { z } from 'zod';
|
|
2
3
|
import { run } from '../app-workspace';
|
|
4
|
+
import { DATABASE_NAME_PATTERN } from '../provision-database/env-file';
|
|
3
5
|
import { boundDatabaseId, provisionFromTool, } from '../provision-database/provision';
|
|
4
|
-
const ProvisionDatabaseZod =
|
|
6
|
+
const ProvisionDatabaseZod = strictObject('the input', {
|
|
5
7
|
database_name: z
|
|
6
8
|
.string()
|
|
7
|
-
.
|
|
8
|
-
|
|
9
|
-
.
|
|
10
|
-
.describe('
|
|
11
|
-
.optional(),
|
|
9
|
+
.min(3)
|
|
10
|
+
.max(63)
|
|
11
|
+
.regex(DATABASE_NAME_PATTERN, { message: 'must start with a letter and hold only letters, digits, _ and -' })
|
|
12
|
+
.describe('3 to 63 characters: a letter, then letters, digits, _ or -. It becomes the subdomain (api-<name>.<domain>), so keep it short and lowercase.'),
|
|
12
13
|
});
|
|
13
14
|
// `exec` runs the prewarm's child processes (clone, install); tests inject it.
|
|
14
15
|
export function createProvisionDatabaseTool(exec = run) {
|
|
15
16
|
return {
|
|
16
17
|
name: 'provision_database',
|
|
17
18
|
label: 'Provision database',
|
|
18
|
-
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write the database binding (DATABASE_ID, DATABASE_NAME, etc.) to the project .env. Run this ONCE before
|
|
19
|
-
promptSnippet: 'provision_database: one-time bootstrap of the project database (owner + modules + .env)
|
|
19
|
+
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write the database binding (DATABASE_ID, DATABASE_NAME, etc.) to the project .env. Run this ONCE before the schema and record tools. If the project is already bound to a live database under the signed-in account it skips. If a call returns while the database is still provisioning, call it again with the same name to keep waiting; a different name starts a second database.',
|
|
20
|
+
promptSnippet: 'provision_database: one-time bootstrap of the project database (owner + modules + .env), before any schema change. Skips when the existing binding is live under the signed-in account. Gated.',
|
|
20
21
|
parameters: ProvisionDatabaseZod,
|
|
22
|
+
effect: 'write',
|
|
21
23
|
async execute(params, ctx) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
const { details, failure } = await provisionFromTool({ databaseName: params.database_name, mode: 'ensure' }, ctx, exec);
|
|
25
|
+
return { content: [{ type: 'text', text: details.message }], details, ...(failure ? { failure } : {}) };
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
// `exec` runs the prewarm's child processes (clone, install); tests inject it.
|
|
30
|
+
export function createReplaceDatabaseTool(exec = run) {
|
|
31
|
+
return {
|
|
32
|
+
name: 'replace_database',
|
|
33
|
+
label: 'Replace database',
|
|
34
|
+
description: 'Bind the project to a new, empty database in place of the one it is bound to: for a bound database that no longer exists on this backend, one that belongs to another account, or a clean rebuild the user asks for. The old .env keys are archived as comments and the old database is kept on the backend; the schema must be built again in the new one, and records do not carry over. Destructive.',
|
|
35
|
+
promptSnippet: 'replace_database: bind the project to a new, empty database in place of a dead, foreign-account or unwanted one (old keys archived, old database kept). Destructive — gated.',
|
|
36
|
+
parameters: ProvisionDatabaseZod,
|
|
37
|
+
effect: 'destructive',
|
|
38
|
+
async execute(params, ctx) {
|
|
39
|
+
// A replace swaps the binding the project has right now; one that changes while this call waits for the project
|
|
40
|
+
// lock is not replaced.
|
|
41
|
+
const replaces = (await boundDatabaseId(ctx.cwd)) ?? null;
|
|
42
|
+
const { details, failure } = await provisionFromTool({ databaseName: params.database_name, mode: 'replace', replaces }, ctx, exec);
|
|
43
|
+
return { content: [{ type: 'text', text: details.message }], details, ...(failure ? { failure } : {}) };
|
|
31
44
|
},
|
|
32
45
|
};
|
|
33
46
|
}
|
|
34
47
|
export const provisionDatabaseTool = createProvisionDatabaseTool();
|
|
48
|
+
export const replaceDatabaseTool = createReplaceDatabaseTool();
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type HarnessTool } from '@agentic-kit/harness';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { prewarmAppWorkspace, type RunCommand } from '../app-workspace';
|
|
4
|
-
|
|
3
|
+
import { type AppKind, prewarmAppWorkspace, type RunCommand } from '../app-workspace';
|
|
4
|
+
import type { ProjectContext } from '../context';
|
|
5
|
+
import { type Result } from '../failure';
|
|
6
|
+
declare const RunCodegenZod: z.ZodObject<{}, z.core.$strict>;
|
|
5
7
|
/** Wall-clock milliseconds per step; a step that didn't run reports 0. */
|
|
6
8
|
export type RunCodegenTimings = {
|
|
7
9
|
scaffoldMs: number;
|
|
@@ -23,7 +25,28 @@ export type RunCodegenDetails = {
|
|
|
23
25
|
timings?: RunCodegenTimings;
|
|
24
26
|
/** The app API answered ACCESS_SUSPENDED: the database is deleted or suspended. */
|
|
25
27
|
code?: 'database-unavailable';
|
|
28
|
+
/** With `app-missing`: the app data plane endpoint the SDK is generated from. */
|
|
29
|
+
appEndpoint?: string;
|
|
26
30
|
};
|
|
31
|
+
/** What run_codegen runs on: the project's database, the app in packages/app, and what it does to the app first. */
|
|
32
|
+
export type RunCodegenReady = {
|
|
33
|
+
context: ProjectContext;
|
|
34
|
+
app: Exclude<AppKind, {
|
|
35
|
+
kind: 'invalid';
|
|
36
|
+
}>;
|
|
37
|
+
/** Whether this host lets run_codegen create packages/app and install its packages (`ToolsHost.prewarmApp`). */
|
|
38
|
+
preparesApp: boolean;
|
|
39
|
+
/** What the plan says it runs, in words: the project's own code generator, and the steps it takes on the app first when the host prepares the app. */
|
|
40
|
+
notes: string[];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* What run_codegen checks before it changes anything: the project's database
|
|
44
|
+
* on the data plane (codegen reads the app's own API, as the Preview does), by
|
|
45
|
+
* name, and the app in packages/app. Its API endpoint is the .env pin when
|
|
46
|
+
* there is one, as the data plane resolves it. A host that prepares no app
|
|
47
|
+
* (`prewarmApp: false`) needs an app there, installed when it is composed.
|
|
48
|
+
*/
|
|
49
|
+
export declare function runCodegenReady(cwd: string): Promise<Result<RunCodegenReady>>;
|
|
27
50
|
export declare function createRunCodegenTool(exec?: RunCommand): HarnessTool<typeof RunCodegenZod, RunCodegenDetails>;
|
|
28
|
-
export declare const runCodegenTool: HarnessTool<z.ZodObject<{}, z.core.$
|
|
51
|
+
export declare const runCodegenTool: HarnessTool<z.ZodObject<{}, z.core.$strict>, RunCodegenDetails>;
|
|
29
52
|
export { prewarmAppWorkspace };
|