@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
package/tools/mutations.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deleteFieldTool = exports.updateFieldTool = exports.createFieldTool = exports.deleteTableTool = void 0;
|
|
3
|
+
exports.deleteFieldTool = exports.deleteFieldReady = exports.retypeFieldTool = exports.retypeFieldReady = exports.updateFieldTool = exports.createFieldTool = exports.deleteTableTool = exports.deleteTableReady = void 0;
|
|
4
|
+
exports.createFieldReady = createFieldReady;
|
|
5
|
+
exports.updateFieldReady = updateFieldReady;
|
|
4
6
|
const harness_1 = require("@agentic-kit/harness");
|
|
5
7
|
const zod_1 = require("zod");
|
|
6
|
-
const
|
|
8
|
+
const field_type_1 = require("../blueprint/field-type");
|
|
9
|
+
const failure_1 = require("../failure");
|
|
10
|
+
const ready_1 = require("../ready");
|
|
7
11
|
const schema_resolve_1 = require("../schema-resolve");
|
|
8
12
|
function ok(message) {
|
|
9
13
|
return {
|
|
@@ -11,223 +15,370 @@ function ok(message) {
|
|
|
11
15
|
details: { success: true, message },
|
|
12
16
|
};
|
|
13
17
|
}
|
|
14
|
-
function fail(
|
|
18
|
+
function fail(failure, errors) {
|
|
19
|
+
return (0, failure_1.failedResult)(failure, {
|
|
20
|
+
success: false,
|
|
21
|
+
message: (0, failure_1.failureText)(failure),
|
|
22
|
+
...(errors ? { errors } : {}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
// The main change happened and the unique constraint step did not.
|
|
26
|
+
function partial(done, constraint, hint, retry) {
|
|
27
|
+
return fail({ code: 'partial', message: `${done} ${constraint.message.replace(/\.?$/, '.')}`, hint, calls: [retry] }, [constraint.message]);
|
|
28
|
+
}
|
|
29
|
+
async function namedReady(cwd, tableName, fieldName) {
|
|
30
|
+
const ready = await (0, ready_1.projectReady)(cwd);
|
|
31
|
+
if (ready.kind === 'failed')
|
|
32
|
+
return ready;
|
|
33
|
+
try {
|
|
34
|
+
const table = (0, schema_resolve_1.resolveTable)(await (0, schema_resolve_1.resolveSchema)(ready.value), tableName);
|
|
35
|
+
return { kind: 'ok', value: { context: ready.value, table, ...(fieldName === undefined ? {} : { field: (0, schema_resolve_1.resolveField)(table, fieldName) }) } };
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
return { kind: 'failed', failure: (0, failure_1.failureFromError)(err, 'Failed to read the schema of the project database') };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// A new field's name is free when the table has no field of the name the backend stores it under.
|
|
42
|
+
function fieldTaken(table, given, stored, except) {
|
|
43
|
+
if (!table.fields.some((field) => field.name === stored && field.id !== except))
|
|
44
|
+
return null;
|
|
45
|
+
const how = stored === given ? '' : `, which is how the backend stores "${given}"`;
|
|
15
46
|
return {
|
|
16
|
-
|
|
17
|
-
|
|
47
|
+
code: 'name-taken',
|
|
48
|
+
message: `Table "${table.name}" already has a field named "${stored}"${how}.`,
|
|
49
|
+
hint: 'Choose another field name; check the names that exist.',
|
|
50
|
+
calls: [failure_1.DESCRIBE_SCHEMA],
|
|
18
51
|
};
|
|
19
52
|
}
|
|
20
53
|
// ── delete_table ──────────────────────────────────────────────────────────
|
|
21
|
-
const DeleteTableZod =
|
|
22
|
-
table_name:
|
|
54
|
+
const DeleteTableZod = (0, harness_1.strictObject)('the input', {
|
|
55
|
+
table_name: (0, harness_1.identifierParam)().describe('Name of the table to delete (snake_case)'),
|
|
23
56
|
});
|
|
57
|
+
/** What delete_table checks before it changes anything: the project, and the table. */
|
|
58
|
+
const deleteTableReady = (params, cwd) => namedReady(cwd, params.table_name);
|
|
59
|
+
exports.deleteTableReady = deleteTableReady;
|
|
24
60
|
exports.deleteTableTool = {
|
|
25
61
|
name: 'delete_table',
|
|
26
62
|
label: 'Delete table',
|
|
27
63
|
description: 'Permanently delete a table and its data from the project database.',
|
|
28
64
|
promptSnippet: 'delete_table: drop an existing table by name. Destructive — gated.',
|
|
29
65
|
parameters: DeleteTableZod,
|
|
66
|
+
effect: 'destructive',
|
|
30
67
|
async execute(params, ctx) {
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
68
|
+
const approved = ctx.toolCallId ? (0, harness_1.takeApprovedTable)(ctx.toolCallId, ctx.cwd) : undefined;
|
|
69
|
+
const ready = await (0, exports.deleteTableReady)(params, ctx.cwd);
|
|
70
|
+
if (ready.kind === 'failed')
|
|
71
|
+
return fail(ready.failure);
|
|
72
|
+
const { context, table } = ready.value;
|
|
73
|
+
if (ctx.toolCallId && (!approved || approved.database.id !== context.databaseId || approved.apiEndpoint !== context.controlApiEndpoint ||
|
|
74
|
+
approved.account.userId !== context.accountUserId || approved.account.agent?.principalId !== context.accountAgent?.principalId ||
|
|
75
|
+
approved.table?.schemaId !== context.schemaId || approved.table.id !== table.id)) {
|
|
76
|
+
return fail({
|
|
77
|
+
code: 'failed',
|
|
78
|
+
message: 'The current table and project do not match the approved delete target.',
|
|
79
|
+
hint: 'Review the current target and approve a new request.',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
34
82
|
try {
|
|
35
|
-
const
|
|
36
|
-
const table = (0, schema_resolve_1.resolveTable)(schema, params.table_name);
|
|
37
|
-
await resolved.context.api.table
|
|
83
|
+
const result = await context.api.table
|
|
38
84
|
.delete({ where: { id: table.id }, select: { id: true } })
|
|
39
85
|
.unwrap();
|
|
86
|
+
if (!result.deleteTable?.table) {
|
|
87
|
+
return fail((0, failure_1.noRowsChanged)(`Failed to delete table "${params.table_name}"`, failure_1.DESCRIBE_SCHEMA));
|
|
88
|
+
}
|
|
40
89
|
return ok(`Deleted table ${params.table_name}`);
|
|
41
90
|
}
|
|
42
91
|
catch (err) {
|
|
43
|
-
return fail(
|
|
92
|
+
return fail((0, failure_1.failureFromError)(err, `Failed to delete table "${params.table_name}"`));
|
|
44
93
|
}
|
|
45
94
|
},
|
|
46
95
|
};
|
|
47
96
|
// ── create_field ──────────────────────────────────────────────────────────
|
|
48
|
-
const CreateFieldZod =
|
|
49
|
-
table_name:
|
|
50
|
-
field_name:
|
|
51
|
-
type:
|
|
52
|
-
.string()
|
|
97
|
+
const CreateFieldZod = (0, harness_1.strictObject)('the input', {
|
|
98
|
+
table_name: (0, harness_1.identifierParam)().describe('Name of the table to add the field to'),
|
|
99
|
+
field_name: (0, harness_1.identifierParam)().describe('Field name in snake_case'),
|
|
100
|
+
type: (0, harness_1.textParam)()
|
|
53
101
|
.describe('PostgreSQL type: text, integer, smallint, numeric, boolean, date, timestamp, timestamptz, time, json, jsonb, inet, email, url, image, upload'),
|
|
54
102
|
is_required: zod_1.z.boolean().describe('Whether the field is NOT NULL. Defaults to false.').optional(),
|
|
55
103
|
default_value: zod_1.z
|
|
56
|
-
.union([zod_1.z.string(), zod_1.z.null()])
|
|
57
|
-
.describe('SQL default expression, e.g. "now()", "\'draft\'"')
|
|
104
|
+
.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()])
|
|
105
|
+
.describe('SQL default expression as text, e.g. "now()", "\'draft\'"; or a number, true or false for a constant')
|
|
58
106
|
.optional(),
|
|
59
107
|
is_unique: zod_1.z
|
|
60
108
|
.boolean()
|
|
61
109
|
.describe('Whether the field has a unique constraint. Defaults to false.')
|
|
62
110
|
.optional(),
|
|
63
111
|
});
|
|
112
|
+
/** What create_field checks before it changes anything: the project, the table, and that the field's stored name is free. */
|
|
113
|
+
async function createFieldReady(params, cwd) {
|
|
114
|
+
const ready = await namedReady(cwd, params.table_name);
|
|
115
|
+
if (ready.kind === 'failed')
|
|
116
|
+
return ready;
|
|
117
|
+
const taken = fieldTaken(ready.value.table, params.field_name, (0, schema_resolve_1.storedName)(params.field_name) ?? params.field_name);
|
|
118
|
+
return taken ? { kind: 'failed', failure: taken } : ready;
|
|
119
|
+
}
|
|
64
120
|
exports.createFieldTool = {
|
|
65
121
|
name: 'create_field',
|
|
66
122
|
label: 'Create field',
|
|
67
123
|
description: 'Add a new field (column) to an existing table in the project database.',
|
|
68
124
|
promptSnippet: 'create_field: add a column to an existing table. Gated.',
|
|
69
125
|
parameters: CreateFieldZod,
|
|
126
|
+
effect: 'write',
|
|
70
127
|
async execute(params, ctx) {
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
return fail(
|
|
74
|
-
const {
|
|
128
|
+
const ready = await createFieldReady(params, ctx.cwd);
|
|
129
|
+
if (ready.kind === 'failed')
|
|
130
|
+
return fail(ready.failure);
|
|
131
|
+
const { context, table } = ready.value;
|
|
132
|
+
const { api, databaseId } = context;
|
|
75
133
|
try {
|
|
76
|
-
const schema = await (0, schema_resolve_1.resolveSchema)(resolved.context);
|
|
77
|
-
const table = (0, schema_resolve_1.resolveTable)(schema, params.table_name);
|
|
78
134
|
const result = await api.field
|
|
79
135
|
.create({
|
|
80
136
|
data: {
|
|
81
137
|
name: params.field_name,
|
|
82
|
-
type: (0,
|
|
138
|
+
type: (0, field_type_1.toFieldType)(params.type),
|
|
83
139
|
tableId: table.id,
|
|
84
140
|
databaseId,
|
|
85
141
|
isRequired: params.is_required ?? false,
|
|
86
|
-
defaultValue: (0,
|
|
142
|
+
defaultValue: (0, field_type_1.toFieldDefault)(params.default_value),
|
|
87
143
|
},
|
|
88
144
|
select: { id: true, name: true, type: true },
|
|
89
145
|
})
|
|
90
146
|
.unwrap();
|
|
91
147
|
const createdId = result.createField?.field?.id;
|
|
92
|
-
if (
|
|
148
|
+
if (!createdId) {
|
|
149
|
+
return fail((0, failure_1.noRowCreated)(`Failed to add field "${params.field_name}" to "${params.table_name}"`, failure_1.DESCRIBE_SCHEMA));
|
|
150
|
+
}
|
|
151
|
+
// The backend stores the name it was given in its own form (inflection.underscore).
|
|
152
|
+
const stored = result.createField.field.name ?? params.field_name;
|
|
153
|
+
const field = stored === params.field_name ? stored : `${stored} (stored from "${params.field_name}")`;
|
|
154
|
+
const added = `Added field ${table.name}.${field}.`;
|
|
155
|
+
if (params.is_unique) {
|
|
156
|
+
const hint = 'Add the unique constraint to the field again.';
|
|
157
|
+
const retry = (0, failure_1.suggest)('update_field', { table_name: table.name, field_name: stored, is_unique: true }, 'Add the unique constraint');
|
|
93
158
|
try {
|
|
94
|
-
await api.uniqueConstraint
|
|
159
|
+
const constraint = await api.uniqueConstraint
|
|
95
160
|
.create({
|
|
96
161
|
data: {
|
|
97
162
|
tableId: table.id,
|
|
98
163
|
databaseId,
|
|
99
164
|
fieldIds: [createdId],
|
|
100
|
-
name: `${table.name}_${
|
|
165
|
+
name: `${table.name}_${stored}_key`,
|
|
101
166
|
type: 'u',
|
|
102
167
|
},
|
|
103
168
|
select: { id: true },
|
|
104
169
|
})
|
|
105
170
|
.unwrap();
|
|
171
|
+
if (!constraint.createUniqueConstraint?.uniqueConstraint) {
|
|
172
|
+
return partial(added, (0, failure_1.noRowCreated)('The unique constraint was not created'), hint, retry);
|
|
173
|
+
}
|
|
106
174
|
}
|
|
107
175
|
catch (err) {
|
|
108
|
-
|
|
176
|
+
return partial(added, (0, failure_1.failureFromError)(err, 'The unique constraint was not created'), hint, retry);
|
|
109
177
|
}
|
|
110
178
|
}
|
|
111
|
-
return ok(
|
|
179
|
+
return ok(added);
|
|
112
180
|
}
|
|
113
181
|
catch (err) {
|
|
114
|
-
return fail(err
|
|
182
|
+
return fail((0, failure_1.failureFromError)(err, `Failed to add field "${params.field_name}" to "${params.table_name}"`, {
|
|
183
|
+
taken: `a field named "${params.field_name}" already exists on "${params.table_name}".`,
|
|
184
|
+
check: failure_1.DESCRIBE_SCHEMA,
|
|
185
|
+
}));
|
|
115
186
|
}
|
|
116
187
|
},
|
|
117
188
|
};
|
|
118
189
|
// ── update_field ──────────────────────────────────────────────────────────
|
|
119
|
-
const UpdateFieldZod =
|
|
120
|
-
table_name:
|
|
121
|
-
field_name:
|
|
122
|
-
new_name:
|
|
123
|
-
new_type: zod_1.z.string().describe('New PostgreSQL type (if changing type)').optional(),
|
|
190
|
+
const UpdateFieldZod = (0, harness_1.strictObject)('the input', {
|
|
191
|
+
table_name: (0, harness_1.identifierParam)().describe('Name of the table containing the field'),
|
|
192
|
+
field_name: (0, harness_1.identifierParam)().describe('Current field name'),
|
|
193
|
+
new_name: (0, harness_1.identifierParam)().describe('New field name in snake_case (if renaming)').optional(),
|
|
124
194
|
is_required: zod_1.z.boolean().describe('Set NOT NULL constraint').optional(),
|
|
125
195
|
default_value: zod_1.z
|
|
126
|
-
.union([zod_1.z.string(), zod_1.z.null()])
|
|
127
|
-
.describe('New SQL default expression, or null to remove')
|
|
196
|
+
.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()])
|
|
197
|
+
.describe('New SQL default expression as text, a number, true or false for a constant, or null to remove')
|
|
128
198
|
.optional(),
|
|
129
199
|
is_unique: zod_1.z.boolean().describe('Set or remove unique constraint').optional(),
|
|
130
200
|
});
|
|
201
|
+
const UPDATE_FIELD_CHANGES = ['new_name', 'is_required', 'default_value', 'is_unique'];
|
|
202
|
+
/** What update_field checks before it changes anything: a change to make, and the project. */
|
|
203
|
+
async function updateFieldReady(params, cwd) {
|
|
204
|
+
if (!UPDATE_FIELD_CHANGES.some((key) => params[key] !== undefined)) {
|
|
205
|
+
return {
|
|
206
|
+
kind: 'failed',
|
|
207
|
+
failure: {
|
|
208
|
+
code: 'invalid-input',
|
|
209
|
+
message: 'The call gives no change: rename the field, or set whether it is required or unique, or its default.',
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const ready = await namedReady(cwd, params.table_name, params.field_name);
|
|
214
|
+
if (ready.kind === 'failed')
|
|
215
|
+
return ready;
|
|
216
|
+
const { table, field } = ready.value;
|
|
217
|
+
// The backend keeps a new name of a field as it is given.
|
|
218
|
+
const taken = params.new_name ? fieldTaken(table, params.new_name, params.new_name, field.id) : null;
|
|
219
|
+
return taken ? { kind: 'failed', failure: taken } : { kind: 'ok', value: { ...ready.value, field } };
|
|
220
|
+
}
|
|
221
|
+
/** Adds or removes the field's unique constraint as asked: whether it changed, or why it could not. */
|
|
222
|
+
async function setUnique(context, table, field, params) {
|
|
223
|
+
if (params.is_unique === undefined)
|
|
224
|
+
return 'unchanged';
|
|
225
|
+
const existingUniqueId = (0, schema_resolve_1.findUniqueConstraintId)(table, field.id);
|
|
226
|
+
try {
|
|
227
|
+
if (params.is_unique && !existingUniqueId) {
|
|
228
|
+
const result = await context.api.uniqueConstraint
|
|
229
|
+
.create({
|
|
230
|
+
data: {
|
|
231
|
+
tableId: table.id,
|
|
232
|
+
databaseId: context.databaseId,
|
|
233
|
+
fieldIds: [field.id],
|
|
234
|
+
name: `${table.name}_${params.new_name ?? field.name}_key`,
|
|
235
|
+
type: 'u',
|
|
236
|
+
},
|
|
237
|
+
select: { id: true },
|
|
238
|
+
})
|
|
239
|
+
.unwrap();
|
|
240
|
+
if (!result.createUniqueConstraint?.uniqueConstraint)
|
|
241
|
+
return (0, failure_1.noRowCreated)('The unique constraint was not added', failure_1.DESCRIBE_SCHEMA);
|
|
242
|
+
return 'changed';
|
|
243
|
+
}
|
|
244
|
+
if (!params.is_unique && existingUniqueId) {
|
|
245
|
+
const result = await context.api.uniqueConstraint
|
|
246
|
+
.delete({ where: { id: existingUniqueId }, select: { id: true } })
|
|
247
|
+
.unwrap();
|
|
248
|
+
if (!result.deleteUniqueConstraint?.uniqueConstraint) {
|
|
249
|
+
return (0, failure_1.noRowsChanged)('The unique constraint was not removed', failure_1.DESCRIBE_SCHEMA);
|
|
250
|
+
}
|
|
251
|
+
return 'changed';
|
|
252
|
+
}
|
|
253
|
+
return 'unchanged';
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
return (0, failure_1.failureFromError)(err, `The unique constraint was not ${params.is_unique ? 'added' : 'removed'}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
131
259
|
exports.updateFieldTool = {
|
|
132
260
|
name: 'update_field',
|
|
133
261
|
label: 'Update field',
|
|
134
|
-
description: 'Modify an existing field: rename
|
|
135
|
-
promptSnippet: 'update_field: rename
|
|
262
|
+
description: 'Modify an existing field: rename it, toggle required or unique, or set or remove its default. Its data stays.',
|
|
263
|
+
promptSnippet: 'update_field: rename an existing column, or change whether it is required or unique, or its default. Gated.',
|
|
136
264
|
parameters: UpdateFieldZod,
|
|
265
|
+
effect: 'write',
|
|
137
266
|
async execute(params, ctx) {
|
|
138
|
-
const
|
|
139
|
-
if (
|
|
140
|
-
return fail(
|
|
141
|
-
const {
|
|
267
|
+
const ready = await updateFieldReady(params, ctx.cwd);
|
|
268
|
+
if (ready.kind === 'failed')
|
|
269
|
+
return fail(ready.failure);
|
|
270
|
+
const { context, table, field } = ready.value;
|
|
271
|
+
const target = `${params.table_name}.${params.field_name}`;
|
|
142
272
|
try {
|
|
143
|
-
const schema = await (0, schema_resolve_1.resolveSchema)(resolved.context);
|
|
144
|
-
const table = (0, schema_resolve_1.resolveTable)(schema, params.table_name);
|
|
145
|
-
const field = (0, schema_resolve_1.resolveField)(table, params.field_name);
|
|
146
273
|
const patch = {};
|
|
147
274
|
if (params.new_name)
|
|
148
275
|
patch.name = params.new_name;
|
|
149
|
-
if (params.new_type)
|
|
150
|
-
patch.type = (0, harness_1.toFieldType)(params.new_type);
|
|
151
276
|
if (params.is_required !== undefined)
|
|
152
277
|
patch.isRequired = params.is_required;
|
|
153
278
|
if (params.default_value !== undefined) {
|
|
154
279
|
patch.defaultValue =
|
|
155
|
-
params.default_value === null ? null : ((0,
|
|
280
|
+
params.default_value === null ? null : ((0, field_type_1.toFieldDefault)(params.default_value) ?? null);
|
|
156
281
|
}
|
|
157
|
-
|
|
158
|
-
|
|
282
|
+
const patched = Object.keys(patch).length > 0;
|
|
283
|
+
if (patched) {
|
|
284
|
+
const result = await context.api.field
|
|
159
285
|
.update({
|
|
160
286
|
where: { id: field.id },
|
|
161
287
|
data: patch,
|
|
162
288
|
select: { id: true, name: true, type: true },
|
|
163
289
|
})
|
|
164
290
|
.unwrap();
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const existingUniqueId = (0, schema_resolve_1.findUniqueConstraintId)(table, field.id);
|
|
168
|
-
if (params.is_unique && !existingUniqueId) {
|
|
169
|
-
try {
|
|
170
|
-
await api.uniqueConstraint
|
|
171
|
-
.create({
|
|
172
|
-
data: {
|
|
173
|
-
tableId: table.id,
|
|
174
|
-
databaseId,
|
|
175
|
-
fieldIds: [field.id],
|
|
176
|
-
name: `${table.name}_${params.new_name ?? field.name}_key`,
|
|
177
|
-
type: 'u',
|
|
178
|
-
},
|
|
179
|
-
select: { id: true },
|
|
180
|
-
})
|
|
181
|
-
.unwrap();
|
|
182
|
-
}
|
|
183
|
-
catch (err) {
|
|
184
|
-
console.warn('Failed to create unique constraint:', err);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
else if (!params.is_unique && existingUniqueId) {
|
|
188
|
-
try {
|
|
189
|
-
await api.uniqueConstraint
|
|
190
|
-
.delete({ where: { id: existingUniqueId }, select: { id: true } })
|
|
191
|
-
.unwrap();
|
|
192
|
-
}
|
|
193
|
-
catch (err) {
|
|
194
|
-
console.warn('Failed to delete unique constraint:', err);
|
|
195
|
-
}
|
|
291
|
+
if (!result.updateField?.field) {
|
|
292
|
+
return fail((0, failure_1.noRowsChanged)(`Failed to update field "${params.field_name}" on "${params.table_name}"`, failure_1.DESCRIBE_SCHEMA));
|
|
196
293
|
}
|
|
197
294
|
}
|
|
198
|
-
|
|
295
|
+
const unique = await setUnique(context, table, field, params);
|
|
296
|
+
if (typeof unique === 'object') {
|
|
297
|
+
if (!patched)
|
|
298
|
+
return fail(unique, [unique.message]);
|
|
299
|
+
const retry = (0, failure_1.suggest)('update_field', { table_name: params.table_name, field_name: params.new_name ?? params.field_name, is_unique: params.is_unique }, 'Retry the unique constraint alone');
|
|
300
|
+
return partial(`Updated field ${target}.`, unique, `Retry the unique constraint alone; the other changes are made.`, retry);
|
|
301
|
+
}
|
|
302
|
+
if (!patched && unique === 'unchanged') {
|
|
303
|
+
return ok(`Field ${target} is already ${params.is_unique ? 'unique' : 'not unique'}; nothing changed.`);
|
|
304
|
+
}
|
|
305
|
+
return ok(`Updated field ${target}`);
|
|
199
306
|
}
|
|
200
307
|
catch (err) {
|
|
201
|
-
return fail(
|
|
308
|
+
return fail((0, failure_1.failureFromError)(err, `Failed to update field "${params.field_name}" on "${params.table_name}"`, {
|
|
309
|
+
...(params.new_name
|
|
310
|
+
? { taken: `a field named "${params.new_name}" already exists on "${params.table_name}".`, check: failure_1.DESCRIBE_SCHEMA }
|
|
311
|
+
: {}),
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
// ── retype_field ──────────────────────────────────────────────────────────
|
|
317
|
+
const RetypeFieldZod = (0, harness_1.strictObject)('the input', {
|
|
318
|
+
table_name: (0, harness_1.identifierParam)().describe('Name of the table containing the field'),
|
|
319
|
+
field_name: (0, harness_1.identifierParam)().describe('Name of the field'),
|
|
320
|
+
type: (0, harness_1.textParam)().describe('The new PostgreSQL type, for example integer or text[]'),
|
|
321
|
+
});
|
|
322
|
+
/** What retype_field checks before it changes anything: the project, the table and the field. */
|
|
323
|
+
const retypeFieldReady = (params, cwd) => namedReady(cwd, params.table_name, params.field_name);
|
|
324
|
+
exports.retypeFieldReady = retypeFieldReady;
|
|
325
|
+
exports.retypeFieldTool = {
|
|
326
|
+
name: 'retype_field',
|
|
327
|
+
label: 'Retype field',
|
|
328
|
+
description: 'Change the type of an existing field. Every value is converted; a value that does not fit fails the change or loses data. Destructive.',
|
|
329
|
+
promptSnippet: 'retype_field: change the type of an existing column; the cast can lose data. Destructive — gated.',
|
|
330
|
+
parameters: RetypeFieldZod,
|
|
331
|
+
effect: 'destructive',
|
|
332
|
+
async execute(params, ctx) {
|
|
333
|
+
const ready = await (0, exports.retypeFieldReady)(params, ctx.cwd);
|
|
334
|
+
if (ready.kind === 'failed')
|
|
335
|
+
return fail(ready.failure);
|
|
336
|
+
const { context, field } = ready.value;
|
|
337
|
+
const action = `Failed to change the type of field "${params.field_name}" on "${params.table_name}"`;
|
|
338
|
+
try {
|
|
339
|
+
const result = await context.api.field
|
|
340
|
+
.update({ where: { id: field.id }, data: { type: (0, field_type_1.toFieldType)(params.type) }, select: { id: true } })
|
|
341
|
+
.unwrap();
|
|
342
|
+
if (!result.updateField?.field)
|
|
343
|
+
return fail((0, failure_1.noRowsChanged)(action, failure_1.DESCRIBE_SCHEMA));
|
|
344
|
+
return ok(`Changed the type of field ${params.table_name}.${params.field_name} to ${params.type}.`);
|
|
345
|
+
}
|
|
346
|
+
catch (err) {
|
|
347
|
+
return fail((0, failure_1.failureFromError)(err, action));
|
|
202
348
|
}
|
|
203
349
|
},
|
|
204
350
|
};
|
|
205
351
|
// ── delete_field ──────────────────────────────────────────────────────────
|
|
206
|
-
const DeleteFieldZod =
|
|
207
|
-
table_name:
|
|
208
|
-
field_name:
|
|
352
|
+
const DeleteFieldZod = (0, harness_1.strictObject)('the input', {
|
|
353
|
+
table_name: (0, harness_1.identifierParam)().describe('Name of the table containing the field'),
|
|
354
|
+
field_name: (0, harness_1.identifierParam)().describe('Name of the field to delete'),
|
|
209
355
|
});
|
|
356
|
+
/** What delete_field checks before it changes anything: the project, the table and the field. */
|
|
357
|
+
const deleteFieldReady = (params, cwd) => namedReady(cwd, params.table_name, params.field_name);
|
|
358
|
+
exports.deleteFieldReady = deleteFieldReady;
|
|
210
359
|
exports.deleteFieldTool = {
|
|
211
360
|
name: 'delete_field',
|
|
212
361
|
label: 'Delete field',
|
|
213
362
|
description: 'Permanently delete a field (column) from an existing table.',
|
|
214
363
|
promptSnippet: 'delete_field: drop a column from a table by name. Destructive — gated.',
|
|
215
364
|
parameters: DeleteFieldZod,
|
|
365
|
+
effect: 'destructive',
|
|
216
366
|
async execute(params, ctx) {
|
|
217
|
-
const
|
|
218
|
-
if (
|
|
219
|
-
return fail(
|
|
367
|
+
const ready = await (0, exports.deleteFieldReady)(params, ctx.cwd);
|
|
368
|
+
if (ready.kind === 'failed')
|
|
369
|
+
return fail(ready.failure);
|
|
370
|
+
const { context, field } = ready.value;
|
|
220
371
|
try {
|
|
221
|
-
const
|
|
222
|
-
const table = (0, schema_resolve_1.resolveTable)(schema, params.table_name);
|
|
223
|
-
const field = (0, schema_resolve_1.resolveField)(table, params.field_name);
|
|
224
|
-
await resolved.context.api.field
|
|
372
|
+
const result = await context.api.field
|
|
225
373
|
.delete({ where: { id: field.id }, select: { id: true } })
|
|
226
374
|
.unwrap();
|
|
227
|
-
|
|
375
|
+
if (!result.deleteField?.field) {
|
|
376
|
+
return fail((0, failure_1.noRowsChanged)(`Failed to delete field "${params.field_name}" from "${params.table_name}"`, failure_1.DESCRIBE_SCHEMA));
|
|
377
|
+
}
|
|
378
|
+
return ok(`Deleted field ${params.table_name}.${params.field_name}.`);
|
|
228
379
|
}
|
|
229
380
|
catch (err) {
|
|
230
|
-
return fail(
|
|
381
|
+
return fail((0, failure_1.failureFromError)(err, `Failed to delete field "${params.field_name}" from "${params.table_name}"`));
|
|
231
382
|
}
|
|
232
383
|
},
|
|
233
384
|
};
|
|
@@ -1,12 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { HarnessTool } from '@agentic-kit/harness';
|
|
2
|
+
import type { BlueprintDefinition } from 'node-type-registry';
|
|
3
|
+
import { type RelatedColumns } from '../access/words';
|
|
4
|
+
import { type Blueprint, BlueprintZod } from '../blueprint/blueprint-schema';
|
|
5
|
+
import type { ProjectContext } from '../context';
|
|
6
|
+
import { type Result } from '../failure';
|
|
7
|
+
export type ProvisionBlueprintReady = ProjectContext & {
|
|
8
|
+
ownerId: string;
|
|
9
|
+
definition: BlueprintDefinition;
|
|
10
|
+
related?: RelatedColumns;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* What provision_blueprint checks before it changes anything: an owned
|
|
14
|
+
* project, a blueprint the registry's JSON Schema takes once its defaults are
|
|
15
|
+
* filled in, rules that fit their nodes and the roles of the database, the
|
|
16
|
+
* schemas it names, none of its new tables yet, and the tables its relations
|
|
17
|
+
* name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function provisionBlueprintReady(params: Blueprint, cwd: string): Promise<Result<ProvisionBlueprintReady>>;
|
|
2
20
|
type ProvisionedField = {
|
|
21
|
+
/** As the backend stored it. */
|
|
3
22
|
name: string;
|
|
23
|
+
/** The name the blueprint gave, when the backend stored another. */
|
|
24
|
+
requestedName?: string;
|
|
4
25
|
type: string;
|
|
5
26
|
isRequired: boolean;
|
|
6
27
|
defaultValue: string | null;
|
|
7
28
|
};
|
|
8
29
|
type ProvisionedTable = {
|
|
30
|
+
/** As the backend stored it. */
|
|
9
31
|
name: string;
|
|
32
|
+
/** The name the blueprint gave, when the backend stored another. */
|
|
33
|
+
requestedName?: string;
|
|
10
34
|
fields: ProvisionedField[];
|
|
11
35
|
policies: string[];
|
|
12
36
|
relationCount: number;
|