@agentic-kit/db-tools 0.12.46 → 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
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type HarnessTool, type ToolCallSuggestion } from '@agentic-kit/harness';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import type { ProjectContext } from '../context';
|
|
4
|
+
import { type Result } from '../failure';
|
|
5
|
+
export type EntityTypeSummary = {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string | null;
|
|
8
|
+
description: string | null;
|
|
9
|
+
prefix: string | null;
|
|
10
|
+
parentEntity: string | null;
|
|
11
|
+
isVisible: boolean | null;
|
|
12
|
+
entityTableName: string | null;
|
|
13
|
+
installedModules: string[] | null;
|
|
14
|
+
};
|
|
15
|
+
export type EntityTypesDetails = {
|
|
16
|
+
success: boolean;
|
|
17
|
+
message: string;
|
|
18
|
+
entityTypes?: EntityTypeSummary[];
|
|
19
|
+
};
|
|
20
|
+
export declare function defaultPrefix(name: string): string;
|
|
21
|
+
export declare const LIST_ENTITY_TYPES: ToolCallSuggestion;
|
|
22
|
+
export type EntityTypeRow = {
|
|
23
|
+
id: string;
|
|
24
|
+
prefix?: string | null;
|
|
25
|
+
outMembershipType?: number | null;
|
|
26
|
+
};
|
|
27
|
+
/** The entity types of the project database: each one's prefix is a team kind, and its out_membership_type the number rules store. */
|
|
28
|
+
export declare function readEntityTypes(context: ProjectContext): Promise<Result<EntityTypeRow[]>>;
|
|
29
|
+
declare const ListEntityTypesZod: z.ZodObject<{}, z.core.$strict>;
|
|
30
|
+
export declare const listEntityTypesTool: HarnessTool<typeof ListEntityTypesZod, EntityTypesDetails>;
|
|
31
|
+
declare const CreateEntityTypeZod: z.ZodObject<{
|
|
32
|
+
name: z.ZodString;
|
|
33
|
+
description: z.ZodOptional<z.ZodString>;
|
|
34
|
+
prefix: z.ZodOptional<z.ZodString>;
|
|
35
|
+
parent_entity: z.ZodOptional<z.ZodString>;
|
|
36
|
+
is_visible: z.ZodOptional<z.ZodBoolean>;
|
|
37
|
+
has_invites: z.ZodOptional<z.ZodBoolean>;
|
|
38
|
+
has_profiles: z.ZodOptional<z.ZodBoolean>;
|
|
39
|
+
has_limits: z.ZodOptional<z.ZodBoolean>;
|
|
40
|
+
has_levels: z.ZodOptional<z.ZodBoolean>;
|
|
41
|
+
has_storage: z.ZodOptional<z.ZodBoolean>;
|
|
42
|
+
}, z.core.$strict>;
|
|
43
|
+
type CreateParams = z.infer<typeof CreateEntityTypeZod>;
|
|
44
|
+
/** What create_entity_type checks before it changes anything: a name, the project, and a prefix no entity type has. */
|
|
45
|
+
export declare function createEntityTypeReady(params: CreateParams, cwd: string): Promise<Result<ProjectContext>>;
|
|
46
|
+
export declare const createEntityTypeTool: HarnessTool<typeof CreateEntityTypeZod, EntityTypesDetails>;
|
|
47
|
+
declare const DeleteEntityTypeZod: z.ZodObject<{
|
|
48
|
+
entity_type_id: z.ZodGUID;
|
|
49
|
+
}, z.core.$strict>;
|
|
50
|
+
type DeleteParams = z.infer<typeof DeleteEntityTypeZod>;
|
|
51
|
+
/** What delete_entity_type checks before it changes anything: the project, and an entity type of that id. */
|
|
52
|
+
export declare function deleteEntityTypeReady(params: DeleteParams, cwd: string): Promise<Result<ProjectContext>>;
|
|
53
|
+
export declare const deleteEntityTypeTool: HarnessTool<typeof DeleteEntityTypeZod, EntityTypesDetails>;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { identifierParam, idParam, strictObject, textParam } from '@agentic-kit/harness';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { failedResult, failureFromError, failureText, noRowCreated, noRowsChanged, suggest } from '../failure';
|
|
4
|
+
import { projectReady } from '../ready';
|
|
5
|
+
const SUMMARY_SELECT = {
|
|
6
|
+
id: true,
|
|
7
|
+
name: true,
|
|
8
|
+
description: true,
|
|
9
|
+
prefix: true,
|
|
10
|
+
parentEntity: true,
|
|
11
|
+
isVisible: true,
|
|
12
|
+
outEntityTableName: true,
|
|
13
|
+
outInstalledModules: true,
|
|
14
|
+
};
|
|
15
|
+
function toSummary(row) {
|
|
16
|
+
return {
|
|
17
|
+
id: row.id,
|
|
18
|
+
name: row.name,
|
|
19
|
+
description: row.description,
|
|
20
|
+
prefix: row.prefix,
|
|
21
|
+
parentEntity: row.parentEntity,
|
|
22
|
+
isVisible: row.isVisible,
|
|
23
|
+
entityTableName: row.outEntityTableName,
|
|
24
|
+
installedModules: row.outInstalledModules,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function formatSummary(s) {
|
|
28
|
+
const parts = [
|
|
29
|
+
`- ${s.name ?? '?'} (id: ${s.id})`,
|
|
30
|
+
` table: ${s.entityTableName ?? '?'}, prefix: ${s.prefix ?? '?'}`,
|
|
31
|
+
];
|
|
32
|
+
if (s.parentEntity)
|
|
33
|
+
parts.push(` parent: ${s.parentEntity}`);
|
|
34
|
+
if (s.description)
|
|
35
|
+
parts.push(` description: ${s.description}`);
|
|
36
|
+
if (s.installedModules?.length)
|
|
37
|
+
parts.push(` modules: ${s.installedModules.join(', ')}`);
|
|
38
|
+
return parts.join('\n');
|
|
39
|
+
}
|
|
40
|
+
export function defaultPrefix(name) {
|
|
41
|
+
return name
|
|
42
|
+
.trim()
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
45
|
+
.replace(/^_+|_+$/g, '');
|
|
46
|
+
}
|
|
47
|
+
export const LIST_ENTITY_TYPES = suggest('list_entity_types', {}, 'See the entity types that exist');
|
|
48
|
+
/** The entity types of the project database: each one's prefix is a team kind, and its out_membership_type the number rules store. */
|
|
49
|
+
export async function readEntityTypes(context) {
|
|
50
|
+
try {
|
|
51
|
+
const result = await context.modules.entityTypeProvision
|
|
52
|
+
.findMany({ where: { databaseId: { equalTo: context.databaseId } }, select: { id: true, prefix: true, outMembershipType: true } })
|
|
53
|
+
.unwrap();
|
|
54
|
+
return { kind: 'ok', value: result.entityTypeProvisions.nodes };
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
return { kind: 'failed', failure: failureFromError(err, 'Failed to read the entity types of the project database') };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function fail(failure) {
|
|
61
|
+
return failedResult(failure, { success: false, message: failureText(failure) });
|
|
62
|
+
}
|
|
63
|
+
const done = (message, entityTypes, calls) => ({
|
|
64
|
+
content: [{ type: 'text', text: message }],
|
|
65
|
+
details: { success: true, message, ...(entityTypes ? { entityTypes } : {}) },
|
|
66
|
+
...(calls ? { calls } : {}),
|
|
67
|
+
});
|
|
68
|
+
// ── list_entity_types ────────────────────────────────────────────────────
|
|
69
|
+
const ListEntityTypesZod = strictObject('the input', {});
|
|
70
|
+
export const listEntityTypesTool = {
|
|
71
|
+
name: 'list_entity_types',
|
|
72
|
+
label: 'List entity types',
|
|
73
|
+
description: 'List the entity types of the project database (organizations, teams, projects, …): each one with its id, prefix, entity table and modules. An entity type is the unit API keys can be scoped to. Read-only.',
|
|
74
|
+
promptSnippet: 'list_entity_types: the entity types (orgs, teams, …) of the project database. Read-only.',
|
|
75
|
+
parameters: ListEntityTypesZod,
|
|
76
|
+
effect: 'read',
|
|
77
|
+
async execute(_params, ctx) {
|
|
78
|
+
const ready = await projectReady(ctx.cwd);
|
|
79
|
+
if (ready.kind === 'failed')
|
|
80
|
+
return fail(ready.failure);
|
|
81
|
+
const { modules, databaseId } = ready.value;
|
|
82
|
+
try {
|
|
83
|
+
const result = await modules.entityTypeProvision.findMany({ where: { databaseId: { equalTo: databaseId } }, select: SUMMARY_SELECT }).unwrap();
|
|
84
|
+
const entityTypes = result.entityTypeProvisions.nodes.map((row) => toSummary(row));
|
|
85
|
+
const message = entityTypes.length === 0
|
|
86
|
+
? 'No entity types yet.'
|
|
87
|
+
: `${entityTypes.length} entity type${entityTypes.length === 1 ? '' : 's'}:\n${entityTypes.map(formatSummary).join('\n')}`;
|
|
88
|
+
return done(message, entityTypes);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
return fail(failureFromError(err, 'Failed to list entity types'));
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
// ── create_entity_type ───────────────────────────────────────────────────
|
|
96
|
+
const CreateEntityTypeZod = strictObject('the input', {
|
|
97
|
+
name: textParam().describe('Entity type name'),
|
|
98
|
+
description: z.string().describe('Human-readable description of the entity type').optional(),
|
|
99
|
+
prefix: identifierParam()
|
|
100
|
+
.describe('Table-name prefix for the provisioned tables (e.g. "org" → org table, org_memberships); defaults to the snake_case of name.')
|
|
101
|
+
.optional(),
|
|
102
|
+
parent_entity: identifierParam()
|
|
103
|
+
.describe('Parent entity table name, for hierarchical types (e.g. a "team" under an "org"); the backend defaults it to the root entity (org).')
|
|
104
|
+
.optional(),
|
|
105
|
+
is_visible: z.boolean().describe('Whether the type shows up in end-user entity listings').optional(),
|
|
106
|
+
has_invites: z.boolean().describe('Provision an invites module ({prefix}_invites + claimed invites)').optional(),
|
|
107
|
+
has_profiles: z.boolean().describe('Provision a profiles table for members of this type').optional(),
|
|
108
|
+
has_limits: z.boolean().describe('Provision usage-limits tracking for this type').optional(),
|
|
109
|
+
has_levels: z.boolean().describe('Provision levels/achievements support for this type').optional(),
|
|
110
|
+
has_storage: z.boolean().describe('Provision a storage module (files + buckets tables) for this type').optional(),
|
|
111
|
+
});
|
|
112
|
+
const prefixOf = (params) => params.prefix?.trim() || defaultPrefix(params.name);
|
|
113
|
+
/** What create_entity_type checks before it changes anything: a name, the project, and a prefix no entity type has. */
|
|
114
|
+
export async function createEntityTypeReady(params, cwd) {
|
|
115
|
+
if (!params.name.trim())
|
|
116
|
+
return { kind: 'failed', failure: { code: 'invalid-input', message: 'The entity type needs a name that is not blank.' } };
|
|
117
|
+
const ready = await projectReady(cwd);
|
|
118
|
+
if (ready.kind === 'failed')
|
|
119
|
+
return ready;
|
|
120
|
+
const read = await readEntityTypes(ready.value);
|
|
121
|
+
if (read.kind === 'failed')
|
|
122
|
+
return read;
|
|
123
|
+
const prefix = prefixOf(params);
|
|
124
|
+
if (read.value.some((type) => type.prefix === prefix)) {
|
|
125
|
+
return {
|
|
126
|
+
kind: 'failed',
|
|
127
|
+
failure: {
|
|
128
|
+
code: 'name-taken',
|
|
129
|
+
message: `Failed to create entity type "${params.name}": an entity type with the prefix "${prefix}" already exists in this database.`,
|
|
130
|
+
hint: 'Choose another name or prefix; check the entity types that exist.',
|
|
131
|
+
calls: [LIST_ENTITY_TYPES],
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return ready;
|
|
136
|
+
}
|
|
137
|
+
export const createEntityTypeTool = {
|
|
138
|
+
name: 'create_entity_type',
|
|
139
|
+
label: 'Create entity type',
|
|
140
|
+
description: 'Create an entity type in the project database (an organization, a team, a project): it provisions its own entity table and the tables that record who belongs to each one. A type cannot be changed once provisioned; to change one, delete it and create a new one.',
|
|
141
|
+
promptSnippet: 'create_entity_type: add an entity type (org, team, …) with its table and memberships. Gated.',
|
|
142
|
+
parameters: CreateEntityTypeZod,
|
|
143
|
+
effect: 'write',
|
|
144
|
+
async execute(params, ctx) {
|
|
145
|
+
const ready = await createEntityTypeReady(params, ctx.cwd);
|
|
146
|
+
if (ready.kind === 'failed')
|
|
147
|
+
return fail(ready.failure);
|
|
148
|
+
const { modules, databaseId } = ready.value;
|
|
149
|
+
const name = params.name.trim();
|
|
150
|
+
const action = `Failed to create entity type "${name}"`;
|
|
151
|
+
try {
|
|
152
|
+
const result = await modules.entityTypeProvision
|
|
153
|
+
.create({
|
|
154
|
+
data: {
|
|
155
|
+
databaseId,
|
|
156
|
+
name,
|
|
157
|
+
prefix: prefixOf(params),
|
|
158
|
+
...(params.description !== undefined && { description: params.description }),
|
|
159
|
+
...(params.parent_entity !== undefined && { parentEntity: params.parent_entity }),
|
|
160
|
+
...(params.is_visible !== undefined && { isVisible: params.is_visible }),
|
|
161
|
+
...(params.has_invites !== undefined && { hasInvites: params.has_invites }),
|
|
162
|
+
...(params.has_profiles !== undefined && { hasProfiles: params.has_profiles }),
|
|
163
|
+
...(params.has_limits !== undefined && { hasLimits: params.has_limits }),
|
|
164
|
+
...(params.has_levels !== undefined && { hasLevels: params.has_levels }),
|
|
165
|
+
...(params.has_storage && { storage: [{}] }),
|
|
166
|
+
},
|
|
167
|
+
select: SUMMARY_SELECT,
|
|
168
|
+
})
|
|
169
|
+
.unwrap();
|
|
170
|
+
const row = result.createEntityTypeProvision?.entityTypeProvision;
|
|
171
|
+
if (!row)
|
|
172
|
+
return fail(noRowCreated(action, LIST_ENTITY_TYPES));
|
|
173
|
+
const summary = toSummary(row);
|
|
174
|
+
const modulesText = summary.installedModules?.length ? `, modules: ${summary.installedModules.join(', ')}` : '';
|
|
175
|
+
return done(`Created entity type "${summary.name}" (id: ${summary.id}) — entity table "${summary.entityTableName}"${modulesText}. Generate the typed SDK again to use it in the app.`, [summary], [suggest('run_codegen', {}, 'Generate the typed SDK with the new entity type')]);
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
return fail(failureFromError(err, action, { taken: `an entity type with the prefix "${prefixOf(params)}" already exists in this database.`, check: LIST_ENTITY_TYPES }));
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
// ── delete_entity_type ───────────────────────────────────────────────────
|
|
183
|
+
const DeleteEntityTypeZod = strictObject('the input', {
|
|
184
|
+
entity_type_id: idParam().describe('The id of the entity type, as the list of entity types gives it'),
|
|
185
|
+
});
|
|
186
|
+
/** What delete_entity_type checks before it changes anything: the project, and an entity type of that id. */
|
|
187
|
+
export async function deleteEntityTypeReady(params, cwd) {
|
|
188
|
+
const ready = await projectReady(cwd);
|
|
189
|
+
if (ready.kind === 'failed')
|
|
190
|
+
return ready;
|
|
191
|
+
const read = await readEntityTypes(ready.value);
|
|
192
|
+
if (read.kind === 'failed')
|
|
193
|
+
return read;
|
|
194
|
+
if (!read.value.some((type) => type.id === params.entity_type_id)) {
|
|
195
|
+
return {
|
|
196
|
+
kind: 'failed',
|
|
197
|
+
failure: {
|
|
198
|
+
code: 'not-found',
|
|
199
|
+
message: `No entity type has the id ${params.entity_type_id} in the project database.`,
|
|
200
|
+
hint: 'Check the ids of the entity types that exist, then retry.',
|
|
201
|
+
calls: [LIST_ENTITY_TYPES],
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return ready;
|
|
206
|
+
}
|
|
207
|
+
export const deleteEntityTypeTool = {
|
|
208
|
+
name: 'delete_entity_type',
|
|
209
|
+
label: 'Delete entity type',
|
|
210
|
+
description: 'Delete the registration of an entity type. Its entity table and the rows in it stay in the database and its API. Destructive.',
|
|
211
|
+
promptSnippet: 'delete_entity_type: remove the registration of an entity type; its table stays. Destructive — gated.',
|
|
212
|
+
parameters: DeleteEntityTypeZod,
|
|
213
|
+
effect: 'destructive',
|
|
214
|
+
async execute(params, ctx) {
|
|
215
|
+
const ready = await deleteEntityTypeReady(params, ctx.cwd);
|
|
216
|
+
if (ready.kind === 'failed')
|
|
217
|
+
return fail(ready.failure);
|
|
218
|
+
const action = `Failed to delete entity type ${params.entity_type_id}`;
|
|
219
|
+
try {
|
|
220
|
+
const result = await ready.value.modules.entityTypeProvision
|
|
221
|
+
.delete({ where: { id: params.entity_type_id }, select: { id: true, name: true, outEntityTableName: true } })
|
|
222
|
+
.unwrap();
|
|
223
|
+
const row = result.deleteEntityTypeProvision?.entityTypeProvision;
|
|
224
|
+
if (!row)
|
|
225
|
+
return fail(noRowsChanged(action, LIST_ENTITY_TYPES));
|
|
226
|
+
const table = row.outEntityTableName ? ` "${row.outEntityTableName}"` : '';
|
|
227
|
+
return done(`Deleted entity type registration "${row.name ?? params.entity_type_id}". Note: the provisioned entity table${table} remains in the API schema with its data.`);
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
return fail(failureFromError(err, action));
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AnyHarnessTool, PlanTableAccess, PlanTarget } from '@agentic-kit/harness';
|
|
2
|
+
import type { Result } from '../failure';
|
|
3
|
+
/**
|
|
4
|
+
* The Constructive database tools, in registration order.
|
|
5
|
+
*
|
|
6
|
+
* Plain `HarnessTool`s: a harness gets them by mapping them into its own tool
|
|
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).
|
|
9
|
+
*/
|
|
10
|
+
export declare const constructiveDbTools: readonly AnyHarnessTool[];
|
|
11
|
+
/** What a plan names that the tool's check resolved: its target, the access of the tables it creates or changes, and what it must say that the input does not. */
|
|
12
|
+
export type Resolved = {
|
|
13
|
+
target: PlanTarget;
|
|
14
|
+
access?: PlanTableAccess[];
|
|
15
|
+
notes?: string[];
|
|
16
|
+
};
|
|
17
|
+
type Precondition = (params: any, cwd: string) => Promise<Result<Resolved>>;
|
|
18
|
+
/**
|
|
19
|
+
* The check each db tool runs first, before it changes anything, by tool name,
|
|
20
|
+
* for the gate to run on the same parsed params; its target is what the plan
|
|
21
|
+
* names. The template catalog belongs to the account, so the template tools
|
|
22
|
+
* name the account, and provision_database the backend and account it
|
|
23
|
+
* provisions under.
|
|
24
|
+
*/
|
|
25
|
+
export declare const toolPreconditions: ReadonlyMap<string, Precondition>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { tablePolicyFromBlueprint } from '../access';
|
|
2
|
+
import { fieldTypeToTypeName, toFieldDefault } from '../blueprint/field-type';
|
|
3
|
+
import { provisionDatabaseReady } from '../provision-database/provision';
|
|
4
|
+
import { accountTarget, databaseTarget, ownedProjectReady, projectReady } from '../ready';
|
|
5
|
+
import { renameNote } from '../schema-resolve';
|
|
6
|
+
import { columnChange, columnPlan, planTableAccess } from '../table-access';
|
|
7
|
+
import { addedByBackend, describePolicyTypeTool, explainAccessTool, readOnlyNotes, setAccessPlan, setAccessReady, setAccessTool } from './access';
|
|
8
|
+
import { addPoliciesPlan, addPoliciesReady, addPoliciesTool } from './add-policies';
|
|
9
|
+
import { addRecordsReady, addRecordsTool } from './add-records';
|
|
10
|
+
import { addRelationReady, addRelationTool } from './add-relation';
|
|
11
|
+
import { createApiKeyReady, createApiKeyTool } from './create-api-key';
|
|
12
|
+
import { describeSchemaTool } from './describe-schema';
|
|
13
|
+
import { createEntityTypeReady, createEntityTypeTool, deleteEntityTypeReady, deleteEntityTypeTool, listEntityTypesTool } from './entity-types';
|
|
14
|
+
import { createFieldReady, createFieldTool, deleteFieldReady, deleteFieldTool, deleteTableReady, deleteTableTool, retypeFieldReady, retypeFieldTool, updateFieldReady, updateFieldTool, } from './mutations';
|
|
15
|
+
import { provisionBlueprintReady, provisionBlueprintTool } from './provision-blueprint';
|
|
16
|
+
import { provisionDatabaseTool, replaceDatabaseTool } from './provision-database';
|
|
17
|
+
import { runCodegenReady, runCodegenTool } from './run-codegen';
|
|
18
|
+
import { applyTemplateReady, applyTemplateTool, createTemplateTool, deleteTemplateTool, listTemplatesTool, templateReady, updateTemplateTool, } from './templates';
|
|
19
|
+
/**
|
|
20
|
+
* The Constructive database tools, in registration order.
|
|
21
|
+
*
|
|
22
|
+
* Plain `HarnessTool`s: a harness gets them by mapping them into its own tool
|
|
23
|
+
* shape, which is the adapter's job, so this package stays free of any harness
|
|
24
|
+
* dependency (see `toPiTool` in `@agentic-kit/pi` for that binding).
|
|
25
|
+
*/
|
|
26
|
+
export const constructiveDbTools = [
|
|
27
|
+
provisionDatabaseTool,
|
|
28
|
+
replaceDatabaseTool,
|
|
29
|
+
describeSchemaTool,
|
|
30
|
+
listTemplatesTool,
|
|
31
|
+
provisionBlueprintTool,
|
|
32
|
+
addRelationTool,
|
|
33
|
+
deleteTableTool,
|
|
34
|
+
createFieldTool,
|
|
35
|
+
updateFieldTool,
|
|
36
|
+
retypeFieldTool,
|
|
37
|
+
deleteFieldTool,
|
|
38
|
+
addPoliciesTool,
|
|
39
|
+
applyTemplateTool,
|
|
40
|
+
createTemplateTool,
|
|
41
|
+
updateTemplateTool,
|
|
42
|
+
deleteTemplateTool,
|
|
43
|
+
addRecordsTool,
|
|
44
|
+
listEntityTypesTool,
|
|
45
|
+
createEntityTypeTool,
|
|
46
|
+
deleteEntityTypeTool,
|
|
47
|
+
createApiKeyTool,
|
|
48
|
+
runCodegenTool,
|
|
49
|
+
explainAccessTool,
|
|
50
|
+
setAccessTool,
|
|
51
|
+
describePolicyTypeTool,
|
|
52
|
+
];
|
|
53
|
+
/** A tool's first check, with what it resolved turned into what its plan names. */
|
|
54
|
+
function targeting(check, target, access, notes) {
|
|
55
|
+
return async (params, cwd) => {
|
|
56
|
+
const ready = await check(params, cwd);
|
|
57
|
+
if (ready.kind === 'failed')
|
|
58
|
+
return ready;
|
|
59
|
+
const said = (notes?.(params) ?? []).filter((note) => note !== undefined);
|
|
60
|
+
return {
|
|
61
|
+
kind: 'ok',
|
|
62
|
+
value: {
|
|
63
|
+
target: target(ready.value),
|
|
64
|
+
...(access ? { access: access(ready.value) } : {}),
|
|
65
|
+
...(said.length ? { notes: said } : {}),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// A new field's or table's name, as the backend will store it.
|
|
71
|
+
const newFieldNote = (params) => [params.field_name ? renameNote('field', params.field_name) : undefined];
|
|
72
|
+
const newRelationNotes = (params) => [
|
|
73
|
+
...newFieldNote(params),
|
|
74
|
+
params.junction_table_name ? renameNote('table', params.junction_table_name) : undefined,
|
|
75
|
+
];
|
|
76
|
+
// The access of each table a blueprint creates: its rules and grants as the backend receives them, with the read-only
|
|
77
|
+
// limit the backend adds beside a team rule, its own fields and the columns its nodes add (a new table has no rows),
|
|
78
|
+
// and the names the backend stores differently.
|
|
79
|
+
const blueprintAccess = ({ definition, related }) => definition.tables.flatMap((table) => {
|
|
80
|
+
if (typeof table.table_name !== 'string')
|
|
81
|
+
return [];
|
|
82
|
+
const policies = (table.policies ?? []);
|
|
83
|
+
const grants = table.grants;
|
|
84
|
+
const rules = policies.map(tablePolicyFromBlueprint);
|
|
85
|
+
// The blueprint was refused before this when a rule reads a column the table will not have.
|
|
86
|
+
const change = columnChange(table.table_name, (table.fields ?? []).map(({ name }) => ({ name })), table.nodes ?? [], rules);
|
|
87
|
+
const columns = change.kind === 'ok' ? change.value : { added: [], reused: [], failsIfRows: [], fields: [], notes: [] };
|
|
88
|
+
const own = (table.fields ?? []).map((field) => ({
|
|
89
|
+
name: field.name,
|
|
90
|
+
required: field.is_required === true,
|
|
91
|
+
default: typeof field.default === 'object' ? field.default : toFieldDefault(field.default) ?? null,
|
|
92
|
+
type: fieldTypeToTypeName(field.type),
|
|
93
|
+
}));
|
|
94
|
+
const access = planTableAccess(table.table_name, undefined, {
|
|
95
|
+
policies: [...rules, ...addedByBackend(policies, grants)],
|
|
96
|
+
grants,
|
|
97
|
+
rls_enabled: table.use_rls !== false,
|
|
98
|
+
fields: [...own, ...columns.fields],
|
|
99
|
+
...(related ? { related } : {}),
|
|
100
|
+
});
|
|
101
|
+
const names = [renameNote('table', table.table_name), ...(table.fields ?? []).map((field) => renameNote('field', field.name))];
|
|
102
|
+
const notes = [...names.filter((note) => note !== undefined), ...readOnlyNotes(policies, grants), ...columns.notes];
|
|
103
|
+
return [{ ...access, ...(table.schema_name ? { schema: table.schema_name } : {}), ...columnPlan(columns, false), ...(notes.length ? { notes } : {}) }];
|
|
104
|
+
});
|
|
105
|
+
const project = (_params, cwd) => projectReady(cwd);
|
|
106
|
+
const onDatabase = (check) => targeting(check, (context) => databaseTarget(context));
|
|
107
|
+
/**
|
|
108
|
+
* The check each db tool runs first, before it changes anything, by tool name,
|
|
109
|
+
* for the gate to run on the same parsed params; its target is what the plan
|
|
110
|
+
* names. The template catalog belongs to the account, so the template tools
|
|
111
|
+
* name the account, and provision_database the backend and account it
|
|
112
|
+
* provisions under.
|
|
113
|
+
*/
|
|
114
|
+
export const toolPreconditions = new Map([
|
|
115
|
+
[provisionDatabaseTool.name, targeting((_params, cwd) => provisionDatabaseReady(false, cwd), (target) => target)],
|
|
116
|
+
[replaceDatabaseTool.name, targeting((_params, cwd) => provisionDatabaseReady(true, cwd), (target) => target)],
|
|
117
|
+
[describeSchemaTool.name, onDatabase(project)],
|
|
118
|
+
[listTemplatesTool.name, targeting(project, accountTarget)],
|
|
119
|
+
[provisionBlueprintTool.name, targeting(provisionBlueprintReady, (context) => databaseTarget(context), blueprintAccess)],
|
|
120
|
+
[addRelationTool.name, targeting(addRelationReady, (context) => databaseTarget(context), undefined, newRelationNotes)],
|
|
121
|
+
[deleteTableTool.name, targeting(deleteTableReady, ({ context, table }) => ({
|
|
122
|
+
...databaseTarget(context),
|
|
123
|
+
table: { id: table.id, schemaId: context.schemaId },
|
|
124
|
+
}))],
|
|
125
|
+
[createFieldTool.name, targeting(createFieldReady, ({ context }) => databaseTarget(context), undefined, newFieldNote)],
|
|
126
|
+
[updateFieldTool.name, targeting(updateFieldReady, ({ context }) => databaseTarget(context))],
|
|
127
|
+
[retypeFieldTool.name, targeting(retypeFieldReady, ({ context }) => databaseTarget(context))],
|
|
128
|
+
[deleteFieldTool.name, targeting(deleteFieldReady, ({ context }) => databaseTarget(context))],
|
|
129
|
+
[addPoliciesTool.name, targeting(addPoliciesReady, ({ context }) => databaseTarget(context), addPoliciesPlan)],
|
|
130
|
+
[applyTemplateTool.name, targeting(applyTemplateReady, ({ context }) => databaseTarget(context), blueprintAccess)],
|
|
131
|
+
[createTemplateTool.name, targeting((_params, cwd) => ownedProjectReady(cwd), accountTarget)],
|
|
132
|
+
[updateTemplateTool.name, targeting(templateReady, ({ context }) => accountTarget(context))],
|
|
133
|
+
[deleteTemplateTool.name, targeting(templateReady, ({ context }) => accountTarget(context))],
|
|
134
|
+
[addRecordsTool.name, targeting(addRecordsReady, ({ context, userId }) => databaseTarget(context, userId))],
|
|
135
|
+
[listEntityTypesTool.name, onDatabase(project)],
|
|
136
|
+
[createEntityTypeTool.name, onDatabase(createEntityTypeReady)],
|
|
137
|
+
[deleteEntityTypeTool.name, onDatabase(deleteEntityTypeReady)],
|
|
138
|
+
[createApiKeyTool.name, targeting(createApiKeyReady, ({ context }) => databaseTarget(context))],
|
|
139
|
+
[
|
|
140
|
+
runCodegenTool.name,
|
|
141
|
+
async (_params, cwd) => {
|
|
142
|
+
const ready = await runCodegenReady(cwd);
|
|
143
|
+
if (ready.kind === 'failed')
|
|
144
|
+
return ready;
|
|
145
|
+
const { context, notes } = ready.value;
|
|
146
|
+
return { kind: 'ok', value: { target: databaseTarget(context), notes } };
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
[explainAccessTool.name, onDatabase(project)],
|
|
150
|
+
[setAccessTool.name, targeting(setAccessReady, ({ context }) => databaseTarget(context), setAccessPlan)],
|
|
151
|
+
]);
|
package/esm/tools/mutations.d.ts
CHANGED
|
@@ -1,35 +1,64 @@
|
|
|
1
1
|
import { type HarnessTool } from '@agentic-kit/harness';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
+
import type { ProjectContext } from '../context';
|
|
4
|
+
import { type Result } from '../failure';
|
|
5
|
+
import { type ResolvedField, type ResolvedTable } from '../schema-resolve';
|
|
3
6
|
export type MutationDetails = {
|
|
4
7
|
success: boolean;
|
|
5
8
|
message: string;
|
|
9
|
+
/** With a partial result: what the backend said about the part that did not happen. */
|
|
10
|
+
errors?: string[];
|
|
11
|
+
};
|
|
12
|
+
/** The project, and the table (and field) a change names, read before any plan. */
|
|
13
|
+
type Named = {
|
|
14
|
+
context: ProjectContext;
|
|
15
|
+
table: ResolvedTable;
|
|
16
|
+
field?: ResolvedField;
|
|
6
17
|
};
|
|
7
18
|
declare const DeleteTableZod: z.ZodObject<{
|
|
8
19
|
table_name: z.ZodString;
|
|
9
|
-
}, z.core.$
|
|
20
|
+
}, z.core.$strict>;
|
|
21
|
+
/** What delete_table checks before it changes anything: the project, and the table. */
|
|
22
|
+
export declare const deleteTableReady: (params: z.infer<typeof DeleteTableZod>, cwd: string) => Promise<Result<Named>>;
|
|
10
23
|
export declare const deleteTableTool: HarnessTool<typeof DeleteTableZod, MutationDetails>;
|
|
11
24
|
declare const CreateFieldZod: z.ZodObject<{
|
|
12
25
|
table_name: z.ZodString;
|
|
13
26
|
field_name: z.ZodString;
|
|
14
27
|
type: z.ZodString;
|
|
15
28
|
is_required: z.ZodOptional<z.ZodBoolean>;
|
|
16
|
-
default_value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
|
|
29
|
+
default_value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
17
30
|
is_unique: z.ZodOptional<z.ZodBoolean>;
|
|
18
|
-
}, z.core.$
|
|
31
|
+
}, z.core.$strict>;
|
|
32
|
+
/** What create_field checks before it changes anything: the project, the table, and that the field's stored name is free. */
|
|
33
|
+
export declare function createFieldReady(params: z.infer<typeof CreateFieldZod>, cwd: string): Promise<Result<Named>>;
|
|
19
34
|
export declare const createFieldTool: HarnessTool<typeof CreateFieldZod, MutationDetails>;
|
|
20
35
|
declare const UpdateFieldZod: z.ZodObject<{
|
|
21
36
|
table_name: z.ZodString;
|
|
22
37
|
field_name: z.ZodString;
|
|
23
38
|
new_name: z.ZodOptional<z.ZodString>;
|
|
24
|
-
new_type: z.ZodOptional<z.ZodString>;
|
|
25
39
|
is_required: z.ZodOptional<z.ZodBoolean>;
|
|
26
|
-
default_value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
|
|
40
|
+
default_value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull]>>;
|
|
27
41
|
is_unique: z.ZodOptional<z.ZodBoolean>;
|
|
28
|
-
}, z.core.$
|
|
42
|
+
}, z.core.$strict>;
|
|
43
|
+
type UpdateFieldParams = z.infer<typeof UpdateFieldZod>;
|
|
44
|
+
/** What update_field checks before it changes anything: a change to make, and the project. */
|
|
45
|
+
export declare function updateFieldReady(params: UpdateFieldParams, cwd: string): Promise<Result<Named & {
|
|
46
|
+
field: ResolvedField;
|
|
47
|
+
}>>;
|
|
29
48
|
export declare const updateFieldTool: HarnessTool<typeof UpdateFieldZod, MutationDetails>;
|
|
49
|
+
declare const RetypeFieldZod: z.ZodObject<{
|
|
50
|
+
table_name: z.ZodString;
|
|
51
|
+
field_name: z.ZodString;
|
|
52
|
+
type: z.ZodString;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
/** What retype_field checks before it changes anything: the project, the table and the field. */
|
|
55
|
+
export declare const retypeFieldReady: (params: z.infer<typeof RetypeFieldZod>, cwd: string) => Promise<Result<Named>>;
|
|
56
|
+
export declare const retypeFieldTool: HarnessTool<typeof RetypeFieldZod, MutationDetails>;
|
|
30
57
|
declare const DeleteFieldZod: z.ZodObject<{
|
|
31
58
|
table_name: z.ZodString;
|
|
32
59
|
field_name: z.ZodString;
|
|
33
|
-
}, z.core.$
|
|
60
|
+
}, z.core.$strict>;
|
|
61
|
+
/** What delete_field checks before it changes anything: the project, the table and the field. */
|
|
62
|
+
export declare const deleteFieldReady: (params: z.infer<typeof DeleteFieldZod>, cwd: string) => Promise<Result<Named>>;
|
|
34
63
|
export declare const deleteFieldTool: HarnessTool<typeof DeleteFieldZod, MutationDetails>;
|
|
35
64
|
export {};
|