@agentic-kit/db-tools 0.15.1 → 0.16.1

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.
Files changed (74) hide show
  1. package/README.md +4 -4
  2. package/blueprint/blueprint-schema.js +3 -1
  3. package/context.d.ts +9 -9
  4. package/context.js +92 -54
  5. package/db-probe.js +2 -2
  6. package/esm/blueprint/blueprint-schema.js +3 -1
  7. package/esm/context.d.ts +9 -9
  8. package/esm/context.js +91 -54
  9. package/esm/db-probe.js +2 -2
  10. package/esm/failure.d.ts +1 -1
  11. package/esm/failure.js +2 -2
  12. package/esm/gate.js +1 -1
  13. package/esm/graphql-http.d.ts +3 -2
  14. package/esm/graphql-http.js +26 -7
  15. package/esm/host.d.ts +20 -7
  16. package/esm/host.js +7 -2
  17. package/esm/index.d.ts +175 -14
  18. package/esm/index.js +5 -15
  19. package/esm/project-file.d.ts +72 -0
  20. package/esm/project-file.js +161 -0
  21. package/esm/provision-database/provision.d.ts +8 -3
  22. package/esm/provision-database/provision.js +63 -94
  23. package/esm/ready.d.ts +7 -3
  24. package/esm/ready.js +3 -3
  25. package/esm/records/rows.d.ts +1 -0
  26. package/esm/records/rows.js +9 -6
  27. package/esm/tools/access.js +1 -1
  28. package/esm/tools/create-api-key.js +2 -2
  29. package/esm/tools/delete-records.d.ts +1 -1
  30. package/esm/tools/delete-records.js +7 -7
  31. package/esm/tools/index.d.ts +195 -2
  32. package/esm/tools/index.js +25 -15
  33. package/esm/tools/mutations.js +7 -6
  34. package/esm/tools/provision-database.js +4 -4
  35. package/esm/tools/run-codegen.d.ts +2 -2
  36. package/esm/tools/run-codegen.js +10 -11
  37. package/esm/tools/update-records.js +1 -1
  38. package/failure.d.ts +1 -1
  39. package/failure.js +2 -2
  40. package/gate.js +1 -1
  41. package/graphql-http.d.ts +3 -2
  42. package/graphql-http.js +26 -7
  43. package/host.d.ts +20 -7
  44. package/host.js +7 -2
  45. package/index.d.ts +175 -14
  46. package/index.js +9 -45
  47. package/package.json +7 -6
  48. package/project-file.d.ts +72 -0
  49. package/project-file.js +178 -0
  50. package/provision-database/provision.d.ts +8 -3
  51. package/provision-database/provision.js +66 -96
  52. package/ready.d.ts +7 -3
  53. package/ready.js +3 -3
  54. package/records/rows.d.ts +1 -0
  55. package/records/rows.js +8 -5
  56. package/tools/access.js +1 -1
  57. package/tools/create-api-key.js +2 -2
  58. package/tools/delete-records.d.ts +1 -1
  59. package/tools/delete-records.js +8 -8
  60. package/tools/index.d.ts +195 -2
  61. package/tools/index.js +53 -43
  62. package/tools/mutations.js +6 -5
  63. package/tools/provision-database.js +5 -5
  64. package/tools/run-codegen.d.ts +2 -2
  65. package/tools/run-codegen.js +8 -9
  66. package/tools/update-records.js +1 -1
  67. package/esm/provision-database/env-file.d.ts +0 -62
  68. package/esm/provision-database/env-file.js +0 -170
  69. package/esm/provision-database/pending-ticket.d.ts +0 -33
  70. package/esm/provision-database/pending-ticket.js +0 -63
  71. package/provision-database/env-file.d.ts +0 -62
  72. package/provision-database/env-file.js +0 -179
  73. package/provision-database/pending-ticket.d.ts +0 -33
  74. package/provision-database/pending-ticket.js +0 -72
package/ready.js CHANGED
@@ -17,13 +17,13 @@ const accountOf = (context) => ({
17
17
  ...(context.accountAgent ? { agent: context.accountAgent } : {}),
18
18
  });
19
19
  /** The bound database a change goes to, at the backend and as the account the context resolved. */
20
- function databaseTarget(context, appUserId) {
20
+ function databaseTarget(context, appUser = {}) {
21
21
  return {
22
22
  kind: 'database',
23
23
  database: { name: context.databaseName, id: context.databaseId },
24
24
  apiEndpoint: context.controlApiEndpoint,
25
25
  account: accountOf(context),
26
- ...(appUserId ? { appUserId } : {}),
26
+ ...(appUser.userId ? { appUserId: appUser.userId, ...(appUser.email ? { appUserEmail: appUser.email } : {}) } : {}),
27
27
  };
28
28
  }
29
29
  /** A change to what the account owns beside its databases (its templates), at the context's backend. */
@@ -47,6 +47,6 @@ async function ownedProjectReady(cwd) {
47
47
  async function appSessionReady(context) {
48
48
  const token = await (0, context_1.resolveDataToken)(context);
49
49
  return token.token
50
- ? { kind: 'ok', value: { token: token.token, userId: token.userId } }
50
+ ? { kind: 'ok', value: { token: token.token, userId: token.userId, email: token.email } }
51
51
  : { kind: 'failed', failure: token.failure };
52
52
  }
package/records/rows.d.ts CHANGED
@@ -36,6 +36,7 @@ export type AppReady = {
36
36
  context: ProjectContext;
37
37
  token: string;
38
38
  userId?: string;
39
+ email?: string;
39
40
  /** The app's own API, as the person signed in to the app. */
40
41
  adapter: ReturnType<typeof statusAdapter>;
41
42
  tables: RawMetaTable[];
package/records/rows.js CHANGED
@@ -62,8 +62,8 @@ async function appReady(cwd, done) {
62
62
  kind: 'failed',
63
63
  failure: {
64
64
  code: 'missing-credentials',
65
- message: 'Cannot resolve the app data endpoint (DATABASE_NAME missing from .env).',
66
- hint: "Add DATABASE_NAME with the database's name to .env, then retry.",
65
+ message: 'Cannot resolve the app data endpoint (the project binding names no database).',
66
+ hint: 'Bind this directory to its database again (the person approves it), then retry.',
67
67
  },
68
68
  };
69
69
  }
@@ -244,7 +244,7 @@ async function writeRecords(verb, tableName, { adapter, table: rawTable }, mutat
244
244
  };
245
245
  const failed = (failure) => recordsFailed(failure, { ...(rows.length > 0 ? details : {}), ...(failedRows.length > 0 ? { failedRows } : {}) });
246
246
  if (suspended) {
247
- const message = rows.length > 0 ? `${exports.UNAVAILABLE} ${rows.length} row(s) were ${done} before it stopped.` : `${exports.UNAVAILABLE} No rows were ${done}.`;
247
+ const message = rows.length > 0 ? `${exports.UNAVAILABLE} ${(0, harness_1.plural)(rows.length, 'row')} ${rows.length === 1 ? 'was' : 'were'} ${done} before it stopped.` : `${exports.UNAVAILABLE} No rows were ${done}.`;
248
248
  return failed({ code: 'database-unavailable', message });
249
249
  }
250
250
  if (rows.length === 0) {
@@ -259,11 +259,14 @@ async function writeRecords(verb, tableName, { adapter, table: rawTable }, mutat
259
259
  hint: `Fix the rows listed in details.failedRows, then call again with only those rows: the ${done} rows stay.`,
260
260
  });
261
261
  }
262
- const message = `${words.done} ${rows.length} row(s) ${words.into} ${tableName}.${verb === 'insert' ? keysLine(rows, primaryKeyOf(rawTable)?.name) : ''}`;
262
+ const message = `${words.done} ${(0, harness_1.plural)(rows.length, 'row')} ${words.into} ${tableName}.${verb === 'insert' ? keysLine(rows, primaryKeyOf(rawTable)?.name) : ''}`;
263
263
  return { content: [{ type: 'text', text: message }], details: { success: true, message, ...details } };
264
264
  }
265
265
  /** The line that gives the new rows' keys, so a later call can point another row at one. */
266
266
  function keysLine(rows, key) {
267
267
  const keys = key ? rows.map((row) => row[key]).filter((value) => value !== undefined && value !== null) : [];
268
- return keys.length ? `\nThe new rows' ${key} values, in insert order: ${keys.map((value) => JSON.stringify(value)).join(', ')}.` : '';
268
+ const values = keys.map((value) => JSON.stringify(value)).join(', ');
269
+ if (keys.length === 0)
270
+ return '';
271
+ return keys.length === 1 ? `\nThe new row's ${key} value: ${values}.` : `\nThe new rows' ${key} values, in insert order: ${values}.`;
269
272
  }
package/tools/access.js CHANGED
@@ -638,7 +638,7 @@ exports.setAccessTool = {
638
638
  description: 'Replace every access rule of a table with a list of rules, in one change; the grants stay, and the columns the rules need are added. The plan marks each rule as kept, added or removed.',
639
639
  promptSnippet: 'set_access: replace the access rules of a table with a list of rules. Gated.',
640
640
  parameters: SetAccessZod,
641
- effect: 'write',
641
+ effect: 'destructive',
642
642
  async execute(params, ctx) {
643
643
  const ready = await setAccessReady(params, ctx.cwd);
644
644
  if (ready.kind === 'failed')
@@ -95,8 +95,8 @@ async function createApiKeyReady(params, cwd) {
95
95
  kind: 'failed',
96
96
  failure: {
97
97
  code: 'missing-credentials',
98
- message: 'Cannot resolve the app auth endpoint (DATABASE_NAME missing from .env).',
99
- hint: "Add DATABASE_NAME with the database's name to .env, then retry.",
98
+ message: 'Cannot resolve the app auth endpoint (the project binding names no database).',
99
+ hint: 'Bind this directory to its database again (the person approves it), then retry.',
100
100
  },
101
101
  };
102
102
  }
@@ -28,6 +28,6 @@ export type DeleteRecordsDetails = {
28
28
  code?: 'database-unavailable';
29
29
  };
30
30
  /** The plan of a records delete: the exact rows as the target, and the card's preview. */
31
- export declare function deleteRecordsPlan({ context, userId, selection, notes }: DeleteRecordsReady): Resolved;
31
+ export declare function deleteRecordsPlan(ready: DeleteRecordsReady): Resolved;
32
32
  export declare const deleteRecordsTool: HarnessTool<typeof DeleteRecordsZod, DeleteRecordsDetails>;
33
33
  export {};
@@ -34,9 +34,10 @@ const DeleteRecordsZod = (0, harness_1.strictObject)('the input', {
34
34
  .describe('The tables to delete rows from, each with exactly one of rows, where, sample or all.'),
35
35
  });
36
36
  /** The plan of a records delete: the exact rows as the target, and the card's preview. */
37
- function deleteRecordsPlan({ context, userId, selection, notes }) {
37
+ function deleteRecordsPlan(ready) {
38
+ const { context, selection, notes } = ready;
38
39
  return {
39
- target: { ...(0, ready_1.databaseTarget)(context, userId), rows: selection.tables.map(({ tableName, keys }) => ({ tableName, keys })) },
40
+ target: { ...(0, ready_1.databaseTarget)(context, ready), rows: selection.tables.map(({ tableName, keys }) => ({ tableName, keys })) },
40
41
  preview: { kind: 'delete-records', ...selection },
41
42
  ...(notes.length ? { notes } : {}),
42
43
  };
@@ -63,7 +64,6 @@ function plannedRows(tables, rows) {
63
64
  }
64
65
  return { kind: 'ok', value: planned };
65
66
  }
66
- const plural = (count, word) => `${count} ${word}${count === 1 ? '' : 's'}`;
67
67
  const NO_ROW_DELETED = 'The backend answered without the deleted row, so it does not exist, or this account may not delete it.';
68
68
  /** One delete per row, in order, until the database is suspended, and the call's result from each row's answer. */
69
69
  async function deleteRows({ adapter, tables }, planned, kept) {
@@ -109,8 +109,8 @@ async function deleteRows({ adapter, tables }, planned, kept) {
109
109
  const details = { deletedCount, tables: [...results, ...kept.map((tableName) => ({ tableName, deleted: 0, kept: true, failed: [] }))] };
110
110
  const done = results.filter((result) => result.deleted > 0);
111
111
  const message = [
112
- done.length ? `Deleted ${(0, harness_1.listed)(done.map((result, index) => `${index === 0 ? plural(result.deleted, 'row') : result.deleted} from ${result.tableName}`))}.` : '',
113
- ...results.filter((result) => result.failed.length > 0).map(({ tableName, failed: [first, ...rest] }) => `${plural(rest.length + 1, 'row')} of ${tableName} ${rest.length ? 'were' : 'was'} not deleted (${first.key}: ${first.message}).`),
112
+ done.length ? `Deleted ${(0, harness_1.listed)(done.map((result, index) => `${index === 0 ? (0, harness_1.plural)(result.deleted, 'row') : result.deleted} from ${result.tableName}`))}.` : '',
113
+ ...results.filter((result) => result.failed.length > 0).map(({ tableName, failed: [first, ...rest] }) => `${(0, harness_1.plural)(rest.length + 1, 'row')} of ${tableName} ${rest.length ? 'were' : 'was'} not deleted (${first.key}: ${first.message}).`),
114
114
  kept.length ? `Nothing was deleted from ${(0, harness_1.listed)(kept)}: the person unticked ${kept.length === 1 ? 'it, or it' : 'them, or they'} had no rows to delete.` : '',
115
115
  ].filter(Boolean).join(' ');
116
116
  const failed = (failure) => (0, rows_1.recordsFailed)(failure, details);
@@ -158,9 +158,9 @@ exports.deleteRecordsTool = {
158
158
  description: "Delete rows from one or more tables in the project's app database: rows named by primary key, the rows whose columns have given values, the sample rows seeded when the app was built, or every row. One call for every table: the person sees the exact rows first and can keep any table.",
159
159
  promptSnippet: 'delete_records: delete rows from one or many tables, by key, by column values, the sample rows, or all rows. Gated.',
160
160
  parameters: DeleteRecordsZod,
161
- effect: 'write',
161
+ effect: 'destructive',
162
162
  async execute(params, ctx) {
163
- const approved = ctx.toolCallId ? (0, harness_1.takeApprovedTable)(ctx.toolCallId, ctx.cwd) : undefined;
163
+ const approved = ctx.approved?.kind === 'database' ? ctx.approved : undefined;
164
164
  const fail = (failure) => (0, rows_1.recordsFailed)(failure, { deletedCount: 0, tables: [] });
165
165
  if (!approved?.rows) {
166
166
  // The same checks as the gate, so a call that cannot run fails the same way; a call that can still deletes
@@ -176,7 +176,7 @@ exports.deleteRecordsTool = {
176
176
  const ready = await (0, rows_1.appReady)(ctx.cwd, 'deleted');
177
177
  if (ready.kind === 'failed')
178
178
  return fail(ready.failure);
179
- if (!sameTarget(approved, (0, ready_1.databaseTarget)(ready.value.context, ready.value.userId))) {
179
+ if (!sameTarget(approved, (0, ready_1.databaseTarget)(ready.value.context, ready.value))) {
180
180
  return fail({
181
181
  code: 'failed',
182
182
  message: 'The current project or app sign-in does not match the approved delete target, so nothing was deleted.',
package/tools/index.d.ts CHANGED
@@ -1,12 +1,205 @@
1
- import type { AnyHarnessTool, ConfirmPreview, PlanTableAccess, PlanTarget } from '@agentic-kit/harness';
1
+ import { type AnyHarnessTool, type ConfirmPreview, type PlanTableAccess, type PlanTarget } from '@agentic-kit/harness';
2
2
  import type { Result } from '../failure';
3
3
  /**
4
4
  * The Constructive database tools, in registration order.
5
5
  *
6
6
  * Plain `HarnessTool`s: a harness gets them by mapping them into its own tool
7
7
  * shape, which is the adapter's job, so this package stays free of any harness
8
- * dependency (see `toPiTool` in `@agentic-kit/pi` for that binding).
8
+ * dependency (see `toPiTool` in `@agentic-kit/pi` for that binding). Each tool
9
+ * that changes something checks at apply that its plan is still the one the
10
+ * person approved (`requireApproval`), when a gated host gives it a call id.
9
11
  */
12
+ export declare const servedTools: {
13
+ provisionDatabaseTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
14
+ database_name: import("zod").ZodString;
15
+ }, import("zod/v4/core").$strict>, import("./provision-database").ProvisionDatabaseDetails>;
16
+ replaceDatabaseTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
17
+ database_name: import("zod").ZodString;
18
+ }, import("zod/v4/core").$strict>, import("./provision-database").ProvisionDatabaseDetails>;
19
+ describeSchemaTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import("./describe-schema").DescribeSchemaDetails>;
20
+ listTemplatesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import("./templates").ListTemplatesDetails>;
21
+ provisionBlueprintTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
22
+ name: import("zod").ZodString;
23
+ description: import("zod").ZodString;
24
+ definition: import("zod").ZodObject<{
25
+ tables: import("zod").ZodArray<import("zod").ZodObject<{
26
+ table_name: import("zod").ZodOptional<import("zod").ZodString>;
27
+ fields: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodObject<{
28
+ name: import("zod").ZodString;
29
+ }, import("zod/v4/core").$loose>>>;
30
+ }, import("zod/v4/core").$loose>>;
31
+ }, import("zod/v4/core").$loose>;
32
+ categories: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
33
+ tags: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
34
+ }, import("zod/v4/core").$strict>, import("./provision-blueprint").ProvisionBlueprintDetails>;
35
+ addRelationTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
36
+ relation_type: import("zod").ZodOptional<import("zod").ZodEnum<{
37
+ belongs_to: "belongs_to";
38
+ many_to_many: "many_to_many";
39
+ }>>;
40
+ source_table: import("zod").ZodString;
41
+ target_table: import("zod").ZodString;
42
+ field_name: import("zod").ZodOptional<import("zod").ZodString>;
43
+ junction_table_name: import("zod").ZodOptional<import("zod").ZodString>;
44
+ delete_action: import("zod").ZodOptional<import("zod").ZodEnum<{
45
+ c: "c";
46
+ n: "n";
47
+ r: "r";
48
+ a: "a";
49
+ d: "d";
50
+ }>>;
51
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
52
+ }, import("zod/v4/core").$strict>, import("./add-relation").AddRelationDetails>;
53
+ deleteTableTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
54
+ table_name: import("zod").ZodString;
55
+ }, import("zod/v4/core").$strict>, import("./mutations").MutationDetails>;
56
+ createFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
57
+ table_name: import("zod").ZodString;
58
+ field_name: import("zod").ZodString;
59
+ type: import("zod").ZodString;
60
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
61
+ default_value: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodBoolean, import("zod").ZodNull]>>;
62
+ is_unique: import("zod").ZodOptional<import("zod").ZodBoolean>;
63
+ }, import("zod/v4/core").$strict>, import("./mutations").MutationDetails>;
64
+ updateFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
65
+ table_name: import("zod").ZodString;
66
+ field_name: import("zod").ZodString;
67
+ new_name: import("zod").ZodOptional<import("zod").ZodString>;
68
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
69
+ default_value: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodBoolean, import("zod").ZodNull]>>;
70
+ is_unique: import("zod").ZodOptional<import("zod").ZodBoolean>;
71
+ }, import("zod/v4/core").$strict>, import("./mutations").MutationDetails>;
72
+ retypeFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
73
+ table_name: import("zod").ZodString;
74
+ field_name: import("zod").ZodString;
75
+ type: import("zod").ZodString;
76
+ }, import("zod/v4/core").$strict>, import("./mutations").MutationDetails>;
77
+ deleteFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
78
+ table_name: import("zod").ZodString;
79
+ field_name: import("zod").ZodString;
80
+ }, import("zod/v4/core").$strict>, import("./mutations").MutationDetails>;
81
+ addPoliciesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
82
+ table_name: import("zod").ZodString;
83
+ policies: import("zod").ZodArray<import("zod").ZodObject<{
84
+ $type: import("zod").ZodString;
85
+ privileges: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
86
+ permissive: import("zod").ZodOptional<import("zod").ZodBoolean>;
87
+ policy_role: import("zod").ZodOptional<import("zod").ZodString>;
88
+ policy_name: import("zod").ZodOptional<import("zod").ZodString>;
89
+ data: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
90
+ }, import("zod/v4/core").$loose>>;
91
+ }, import("zod/v4/core").$strict>, import("./add-policies").AddPoliciesDetails>;
92
+ applyTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
93
+ templateName: import("zod").ZodString;
94
+ nameOverride: import("zod").ZodOptional<import("zod").ZodString>;
95
+ }, import("zod/v4/core").$strict>, import("./templates").TemplateDetails>;
96
+ createTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
97
+ displayName: import("zod").ZodString;
98
+ blueprintName: import("zod").ZodOptional<import("zod").ZodString>;
99
+ description: import("zod").ZodOptional<import("zod").ZodString>;
100
+ categories: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
101
+ }, import("zod/v4/core").$strict>, import("./templates").CreateTemplateDetails>;
102
+ updateTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
103
+ templateName: import("zod").ZodString;
104
+ patch: import("zod").ZodObject<{
105
+ displayName: import("zod").ZodOptional<import("zod").ZodString>;
106
+ description: import("zod").ZodOptional<import("zod").ZodString>;
107
+ categories: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
108
+ tags: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
109
+ visibility: import("zod").ZodOptional<import("zod").ZodEnum<{
110
+ public: "public";
111
+ private: "private";
112
+ }>>;
113
+ }, import("zod/v4/core").$strict>;
114
+ }, import("zod/v4/core").$strict>, import("./templates").TemplateDetails>;
115
+ deleteTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
116
+ templateName: import("zod").ZodString;
117
+ }, import("zod/v4/core").$strict>, import("./templates").TemplateDetails>;
118
+ addRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
119
+ table_name: import("zod").ZodString;
120
+ records: import("zod").ZodArray<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
121
+ }, import("zod/v4/core").$strict>, import("..").RecordsDetails>;
122
+ updateRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
123
+ table_name: import("zod").ZodString;
124
+ records: import("zod").ZodArray<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
125
+ }, import("zod/v4/core").$strict>, import("..").RecordsDetails>;
126
+ deleteRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
127
+ tables: import("zod").ZodArray<import("zod").ZodObject<{
128
+ table_name: import("zod").ZodString;
129
+ rows: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber]>>>;
130
+ where: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
131
+ sample: import("zod").ZodOptional<import("zod").ZodLiteral<true>>;
132
+ all: import("zod").ZodOptional<import("zod").ZodLiteral<true>>;
133
+ }, import("zod/v4/core").$strict>>;
134
+ }, import("zod/v4/core").$strict>, import("./delete-records").DeleteRecordsDetails>;
135
+ listEntityTypesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import("./entity-types").EntityTypesDetails>;
136
+ createEntityTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
137
+ name: import("zod").ZodString;
138
+ description: import("zod").ZodOptional<import("zod").ZodString>;
139
+ prefix: import("zod").ZodOptional<import("zod").ZodString>;
140
+ parent_entity: import("zod").ZodOptional<import("zod").ZodString>;
141
+ is_visible: import("zod").ZodOptional<import("zod").ZodBoolean>;
142
+ has_invites: import("zod").ZodOptional<import("zod").ZodBoolean>;
143
+ has_profiles: import("zod").ZodOptional<import("zod").ZodBoolean>;
144
+ has_limits: import("zod").ZodOptional<import("zod").ZodBoolean>;
145
+ has_levels: import("zod").ZodOptional<import("zod").ZodBoolean>;
146
+ has_storage: import("zod").ZodOptional<import("zod").ZodBoolean>;
147
+ }, import("zod/v4/core").$strict>, import("./entity-types").EntityTypesDetails>;
148
+ deleteEntityTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
149
+ entity_type_id: import("zod").ZodGUID;
150
+ }, import("zod/v4/core").$strict>, import("./entity-types").EntityTypesDetails>;
151
+ createApiKeyTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
152
+ key_name: import("zod").ZodString;
153
+ principal_name: import("zod").ZodOptional<import("zod").ZodString>;
154
+ entity_ids: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodGUID>>;
155
+ read_only: import("zod").ZodOptional<import("zod").ZodBoolean>;
156
+ expires_in_days: import("zod").ZodOptional<import("zod").ZodNumber>;
157
+ }, import("zod/v4/core").$strict>, import("./create-api-key").CreateApiKeyDetails>;
158
+ runCodegenTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import("./run-codegen").RunCodegenDetails>;
159
+ explainAccessTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
160
+ table_name: import("zod").ZodOptional<import("zod").ZodString>;
161
+ }, import("zod/v4/core").$strict>, {
162
+ tables: import("./access").ExplainedTable[];
163
+ }>;
164
+ setAccessTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
165
+ table_name: import("zod").ZodString;
166
+ policies: import("zod").ZodArray<import("zod").ZodObject<{
167
+ $type: import("zod").ZodString;
168
+ privileges: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
169
+ permissive: import("zod").ZodOptional<import("zod").ZodBoolean>;
170
+ policy_role: import("zod").ZodOptional<import("zod").ZodString>;
171
+ policy_name: import("zod").ZodOptional<import("zod").ZodString>;
172
+ data: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
173
+ }, import("zod/v4/core").$loose>>;
174
+ }, import("zod/v4/core").$strict>, {
175
+ success: boolean;
176
+ message: string;
177
+ table?: string;
178
+ access?: import("./access").ExplainedTable;
179
+ }>;
180
+ describePolicyTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
181
+ type: import("zod").ZodOptional<import("zod").ZodEnum<{
182
+ [x: string]: string;
183
+ }>>;
184
+ }, import("zod/v4/core").$strict>, {
185
+ types?: {
186
+ name: string;
187
+ summary?: string;
188
+ tableKind?: string;
189
+ }[];
190
+ type?: {
191
+ name: string;
192
+ summary?: string;
193
+ tableKind?: string;
194
+ } & {
195
+ parameterSchema: unknown;
196
+ useWhen: string[];
197
+ avoidWhen: string[];
198
+ risks: string[];
199
+ dataNode?: unknown;
200
+ };
201
+ }>;
202
+ };
10
203
  export declare const constructiveDbTools: readonly AnyHarnessTool[];
11
204
  /**
12
205
  * What a plan names that the tool's check resolved: its target, the access of the tables it creates or changes, what
package/tools/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toolPreconditions = exports.constructiveDbTools = void 0;
3
+ exports.toolPreconditions = exports.constructiveDbTools = exports.servedTools = void 0;
4
+ const harness_1 = require("@agentic-kit/harness");
4
5
  const access_1 = require("../access");
5
6
  const field_type_1 = require("../blueprint/field-type");
6
7
  const provision_1 = require("../provision-database/provision");
@@ -27,37 +28,44 @@ const update_records_1 = require("./update-records");
27
28
  *
28
29
  * Plain `HarnessTool`s: a harness gets them by mapping them into its own tool
29
30
  * shape, which is the adapter's job, so this package stays free of any harness
30
- * dependency (see `toPiTool` in `@agentic-kit/pi` for that binding).
31
+ * dependency (see `toPiTool` in `@agentic-kit/pi` for that binding). Each tool
32
+ * that changes something checks at apply that its plan is still the one the
33
+ * person approved (`requireApproval`), when a gated host gives it a call id.
31
34
  */
32
- exports.constructiveDbTools = [
33
- provision_database_1.provisionDatabaseTool,
34
- provision_database_1.replaceDatabaseTool,
35
- describe_schema_1.describeSchemaTool,
36
- templates_1.listTemplatesTool,
37
- provision_blueprint_1.provisionBlueprintTool,
38
- add_relation_1.addRelationTool,
39
- mutations_1.deleteTableTool,
40
- mutations_1.createFieldTool,
41
- mutations_1.updateFieldTool,
42
- mutations_1.retypeFieldTool,
43
- mutations_1.deleteFieldTool,
44
- add_policies_1.addPoliciesTool,
45
- templates_1.applyTemplateTool,
46
- templates_1.createTemplateTool,
47
- templates_1.updateTemplateTool,
48
- templates_1.deleteTemplateTool,
49
- add_records_1.addRecordsTool,
50
- update_records_1.updateRecordsTool,
51
- delete_records_1.deleteRecordsTool,
52
- entity_types_1.listEntityTypesTool,
53
- entity_types_1.createEntityTypeTool,
54
- entity_types_1.deleteEntityTypeTool,
55
- create_api_key_1.createApiKeyTool,
56
- run_codegen_1.runCodegenTool,
57
- access_2.explainAccessTool,
58
- access_2.setAccessTool,
59
- access_2.describePolicyTypeTool,
60
- ];
35
+ exports.servedTools = gated({
36
+ provisionDatabaseTool: provision_database_1.provisionDatabaseTool,
37
+ replaceDatabaseTool: provision_database_1.replaceDatabaseTool,
38
+ describeSchemaTool: describe_schema_1.describeSchemaTool,
39
+ listTemplatesTool: templates_1.listTemplatesTool,
40
+ provisionBlueprintTool: provision_blueprint_1.provisionBlueprintTool,
41
+ addRelationTool: add_relation_1.addRelationTool,
42
+ deleteTableTool: mutations_1.deleteTableTool,
43
+ createFieldTool: mutations_1.createFieldTool,
44
+ updateFieldTool: mutations_1.updateFieldTool,
45
+ retypeFieldTool: mutations_1.retypeFieldTool,
46
+ deleteFieldTool: mutations_1.deleteFieldTool,
47
+ addPoliciesTool: add_policies_1.addPoliciesTool,
48
+ applyTemplateTool: templates_1.applyTemplateTool,
49
+ createTemplateTool: templates_1.createTemplateTool,
50
+ updateTemplateTool: templates_1.updateTemplateTool,
51
+ deleteTemplateTool: templates_1.deleteTemplateTool,
52
+ addRecordsTool: add_records_1.addRecordsTool,
53
+ updateRecordsTool: update_records_1.updateRecordsTool,
54
+ deleteRecordsTool: delete_records_1.deleteRecordsTool,
55
+ listEntityTypesTool: entity_types_1.listEntityTypesTool,
56
+ createEntityTypeTool: entity_types_1.createEntityTypeTool,
57
+ deleteEntityTypeTool: entity_types_1.deleteEntityTypeTool,
58
+ createApiKeyTool: create_api_key_1.createApiKeyTool,
59
+ runCodegenTool: run_codegen_1.runCodegenTool,
60
+ explainAccessTool: access_2.explainAccessTool,
61
+ setAccessTool: access_2.setAccessTool,
62
+ describePolicyTypeTool: access_2.describePolicyTypeTool,
63
+ });
64
+ exports.constructiveDbTools = Object.values(exports.servedTools);
65
+ /** The tools by name, each that changes something wrapped by `requireApproval`, so no unwrapped one leaves the package. */
66
+ function gated(tools) {
67
+ return Object.fromEntries(Object.entries(tools).map(([name, tool]) => [name, (0, harness_1.requireApproval)(tool)]));
68
+ }
61
69
  /** A tool's first check, with what it resolved turned into what its plan names. */
62
70
  function targeting(check, target, access, notes) {
63
71
  return async (params, cwd) => {
@@ -112,6 +120,11 @@ const blueprintAccess = ({ definition, related }) => definition.tables.flatMap((
112
120
  });
113
121
  const project = (_params, cwd) => (0, ready_1.projectReady)(cwd);
114
122
  const onDatabase = (check) => targeting(check, (context) => (0, ready_1.databaseTarget)(context));
123
+ // A call on an existing table (and field) names it by id, so a plan made for one cannot apply to one that replaced it.
124
+ const onTable = ({ context, table, field }) => ({
125
+ ...(0, ready_1.databaseTarget)(context),
126
+ table: { id: table.id, schemaId: context.schemaId, ...(field ? { fieldId: field.id } : {}) },
127
+ });
115
128
  /**
116
129
  * The check each db tool runs first, before it changes anything, by tool name,
117
130
  * for the gate to run on the same parsed params; its target is what the plan
@@ -126,21 +139,18 @@ exports.toolPreconditions = new Map([
126
139
  [templates_1.listTemplatesTool.name, targeting(project, ready_1.accountTarget)],
127
140
  [provision_blueprint_1.provisionBlueprintTool.name, targeting(provision_blueprint_1.provisionBlueprintReady, (context) => (0, ready_1.databaseTarget)(context), blueprintAccess)],
128
141
  [add_relation_1.addRelationTool.name, targeting(add_relation_1.addRelationReady, (context) => (0, ready_1.databaseTarget)(context), undefined, newRelationNotes)],
129
- [mutations_1.deleteTableTool.name, targeting(mutations_1.deleteTableReady, ({ context, table }) => ({
130
- ...(0, ready_1.databaseTarget)(context),
131
- table: { id: table.id, schemaId: context.schemaId },
132
- }))],
133
- [mutations_1.createFieldTool.name, targeting(mutations_1.createFieldReady, ({ context }) => (0, ready_1.databaseTarget)(context), undefined, newFieldNote)],
134
- [mutations_1.updateFieldTool.name, targeting(mutations_1.updateFieldReady, ({ context }) => (0, ready_1.databaseTarget)(context))],
135
- [mutations_1.retypeFieldTool.name, targeting(mutations_1.retypeFieldReady, ({ context }) => (0, ready_1.databaseTarget)(context))],
136
- [mutations_1.deleteFieldTool.name, targeting(mutations_1.deleteFieldReady, ({ context }) => (0, ready_1.databaseTarget)(context))],
137
- [add_policies_1.addPoliciesTool.name, targeting(add_policies_1.addPoliciesReady, ({ context }) => (0, ready_1.databaseTarget)(context), add_policies_1.addPoliciesPlan)],
142
+ [mutations_1.deleteTableTool.name, targeting(mutations_1.deleteTableReady, onTable)],
143
+ [mutations_1.createFieldTool.name, targeting(mutations_1.createFieldReady, onTable, undefined, newFieldNote)],
144
+ [mutations_1.updateFieldTool.name, targeting(mutations_1.updateFieldReady, onTable)],
145
+ [mutations_1.retypeFieldTool.name, targeting(mutations_1.retypeFieldReady, onTable)],
146
+ [mutations_1.deleteFieldTool.name, targeting(mutations_1.deleteFieldReady, onTable)],
147
+ [add_policies_1.addPoliciesTool.name, targeting(add_policies_1.addPoliciesReady, onTable, add_policies_1.addPoliciesPlan)],
138
148
  [templates_1.applyTemplateTool.name, targeting(templates_1.applyTemplateReady, ({ context }) => (0, ready_1.databaseTarget)(context), blueprintAccess)],
139
149
  [templates_1.createTemplateTool.name, targeting((_params, cwd) => (0, ready_1.ownedProjectReady)(cwd), ready_1.accountTarget)],
140
150
  [templates_1.updateTemplateTool.name, targeting(templates_1.templateReady, ({ context }) => (0, ready_1.accountTarget)(context))],
141
151
  [templates_1.deleteTemplateTool.name, targeting(templates_1.templateReady, ({ context }) => (0, ready_1.accountTarget)(context))],
142
- [add_records_1.addRecordsTool.name, targeting(add_records_1.addRecordsReady, ({ context, userId }) => (0, ready_1.databaseTarget)(context, userId))],
143
- [update_records_1.updateRecordsTool.name, targeting(update_records_1.updateRecordsReady, ({ context, userId }) => (0, ready_1.databaseTarget)(context, userId))],
152
+ [add_records_1.addRecordsTool.name, targeting(add_records_1.addRecordsReady, (ready) => (0, ready_1.databaseTarget)(ready.context, ready))],
153
+ [update_records_1.updateRecordsTool.name, targeting(update_records_1.updateRecordsReady, (ready) => (0, ready_1.databaseTarget)(ready.context, ready))],
144
154
  [
145
155
  delete_records_1.deleteRecordsTool.name,
146
156
  async (params, cwd) => {
@@ -163,5 +173,5 @@ exports.toolPreconditions = new Map([
163
173
  },
164
174
  ],
165
175
  [access_2.explainAccessTool.name, onDatabase(project)],
166
- [access_2.setAccessTool.name, targeting(access_2.setAccessReady, ({ context }) => (0, ready_1.databaseTarget)(context), access_2.setAccessPlan)],
176
+ [access_2.setAccessTool.name, targeting(access_2.setAccessReady, onTable, access_2.setAccessPlan)],
167
177
  ]);
@@ -65,16 +65,17 @@ exports.deleteTableTool = {
65
65
  parameters: DeleteTableZod,
66
66
  effect: 'destructive',
67
67
  async execute(params, ctx) {
68
- const approved = ctx.toolCallId ? (0, harness_1.takeApprovedTable)(ctx.toolCallId, ctx.cwd) : undefined;
69
68
  const ready = await (0, exports.deleteTableReady)(params, ctx.cwd);
70
69
  if (ready.kind === 'failed')
71
70
  return fail(ready.failure);
72
71
  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)) {
72
+ const approved = ctx.approved;
73
+ if ((ctx.toolCallId || approved) && (approved?.kind !== 'database' || approved.database.id !== context.databaseId ||
74
+ approved.apiEndpoint !== context.controlApiEndpoint || approved.account.userId !== context.accountUserId ||
75
+ approved.account.agent?.principalId !== context.accountAgent?.principalId ||
76
+ approved.table?.schemaId !== context.schemaId || approved.table?.id !== table.id)) {
76
77
  return fail({
77
- code: 'failed',
78
+ code: 'plan-changed',
78
79
  message: 'The current table and project do not match the approved delete target.',
79
80
  hint: 'Review the current target and approve a new request.',
80
81
  });
@@ -6,14 +6,14 @@ exports.createReplaceDatabaseTool = createReplaceDatabaseTool;
6
6
  const harness_1 = require("@agentic-kit/harness");
7
7
  const zod_1 = require("zod");
8
8
  const app_workspace_1 = require("../app-workspace");
9
- const env_file_1 = require("../provision-database/env-file");
9
+ const project_file_1 = require("../project-file");
10
10
  const provision_1 = require("../provision-database/provision");
11
11
  const ProvisionDatabaseZod = (0, harness_1.strictObject)('the input', {
12
12
  database_name: zod_1.z
13
13
  .string()
14
14
  .min(3)
15
15
  .max(63)
16
- .regex(env_file_1.DATABASE_NAME_PATTERN, { message: 'must start with a letter and hold only letters, digits, _ and -' })
16
+ .regex(project_file_1.DATABASE_NAME_PATTERN, { message: 'must start with a letter and hold only letters, digits, _ and -' })
17
17
  .describe('3 to 63 characters: a letter, then letters, digits, _ or -. It becomes the subdomain (api-<name>.<domain>), so keep it short and lowercase.'),
18
18
  });
19
19
  // `exec` runs the prewarm's child processes (clone, install); tests inject it.
@@ -21,8 +21,8 @@ function createProvisionDatabaseTool(exec = app_workspace_1.run) {
21
21
  return {
22
22
  name: 'provision_database',
23
23
  label: 'Provision database',
24
- 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.',
25
- 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.',
24
+ description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write the database binding to .constructive/project.json in the project. 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.',
25
+ promptSnippet: 'provision_database: one-time bootstrap of the project database (owner + modules + binding), before any schema change. Skips when the existing binding is live under the signed-in account. Gated.',
26
26
  parameters: ProvisionDatabaseZod,
27
27
  effect: 'write',
28
28
  async execute(params, ctx) {
@@ -36,7 +36,7 @@ function createReplaceDatabaseTool(exec = app_workspace_1.run) {
36
36
  return {
37
37
  name: 'replace_database',
38
38
  label: 'Replace database',
39
- 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.',
39
+ 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 binding is archived in .constructive/project.json 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.',
40
40
  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.',
41
41
  parameters: ProvisionDatabaseZod,
42
42
  effect: 'destructive',
@@ -42,8 +42,8 @@ export type RunCodegenReady = {
42
42
  /**
43
43
  * What run_codegen checks before it changes anything: the project's database
44
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
45
+ * name, and the app in packages/app. Its API endpoint is the one the data
46
+ * plane resolves. A host that prepares no app
47
47
  * (`prewarmApp: false`) needs an app there, installed when it is composed.
48
48
  */
49
49
  export declare function runCodegenReady(cwd: string): Promise<Result<RunCodegenReady>>;