@agentic-kit/pi 0.17.0 → 0.18.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.
Files changed (136) hide show
  1. package/README.md +5 -7
  2. package/confirm-gate.d.ts +1 -2
  3. package/esm/confirm-gate.d.ts +1 -2
  4. package/esm/host.d.ts +5 -110
  5. package/esm/host.js +5 -18
  6. package/esm/index.d.ts +17 -9
  7. package/esm/index.js +20 -38
  8. package/esm/pi-tool.d.ts +16 -0
  9. package/esm/pi-tool.js +32 -0
  10. package/host.d.ts +5 -110
  11. package/host.js +10 -22
  12. package/index.d.ts +17 -9
  13. package/index.js +47 -62
  14. package/package.json +6 -10
  15. package/pi-tool.d.ts +16 -0
  16. package/pi-tool.js +36 -0
  17. package/app-workspace.d.ts +0 -16
  18. package/app-workspace.js +0 -145
  19. package/context.d.ts +0 -62
  20. package/context.js +0 -282
  21. package/db-probe.d.ts +0 -30
  22. package/db-probe.js +0 -50
  23. package/esm/app-workspace.d.ts +0 -16
  24. package/esm/app-workspace.js +0 -134
  25. package/esm/context.d.ts +0 -62
  26. package/esm/context.js +0 -270
  27. package/esm/db-probe.d.ts +0 -30
  28. package/esm/db-probe.js +0 -47
  29. package/esm/policy/add-policies-to-table.d.ts +0 -26
  30. package/esm/policy/add-policies-to-table.js +0 -66
  31. package/esm/policy/provision-helpers.d.ts +0 -38
  32. package/esm/policy/provision-helpers.js +0 -50
  33. package/esm/provision-database/credential.d.ts +0 -14
  34. package/esm/provision-database/credential.js +0 -19
  35. package/esm/provision-database/env-file.d.ts +0 -37
  36. package/esm/provision-database/env-file.js +0 -85
  37. package/esm/provision-database/manifest.d.ts +0 -10
  38. package/esm/provision-database/manifest.js +0 -48
  39. package/esm/provision-database/pg-fixups.d.ts +0 -16
  40. package/esm/provision-database/pg-fixups.js +0 -132
  41. package/esm/provision-database/preset-match.d.ts +0 -13
  42. package/esm/provision-database/preset-match.js +0 -66
  43. package/esm/provision-database/presets.d.ts +0 -4
  44. package/esm/provision-database/presets.js +0 -13
  45. package/esm/provision-database/request-database.d.ts +0 -61
  46. package/esm/provision-database/request-database.js +0 -130
  47. package/esm/provision-database/resolve.d.ts +0 -27
  48. package/esm/provision-database/resolve.js +0 -49
  49. package/esm/records/meta.d.ts +0 -41
  50. package/esm/records/meta.js +0 -167
  51. package/esm/run-codegen/barrels.d.ts +0 -3
  52. package/esm/run-codegen/barrels.js +0 -48
  53. package/esm/run-codegen/endpoints.d.ts +0 -8
  54. package/esm/run-codegen/endpoints.js +0 -46
  55. package/esm/schema-resolve.d.ts +0 -23
  56. package/esm/schema-resolve.js +0 -69
  57. package/esm/tool-schema.d.ts +0 -3
  58. package/esm/tool-schema.js +0 -10
  59. package/esm/tools/add-policies.d.ts +0 -8
  60. package/esm/tools/add-policies.js +0 -96
  61. package/esm/tools/add-records.d.ts +0 -14
  62. package/esm/tools/add-records.js +0 -124
  63. package/esm/tools/add-relation-schema.d.ts +0 -30
  64. package/esm/tools/add-relation-schema.js +0 -58
  65. package/esm/tools/add-relation.d.ts +0 -10
  66. package/esm/tools/add-relation.js +0 -91
  67. package/esm/tools/create-api-key.d.ts +0 -18
  68. package/esm/tools/create-api-key.js +0 -216
  69. package/esm/tools/describe-schema.d.ts +0 -18
  70. package/esm/tools/describe-schema.js +0 -132
  71. package/esm/tools/manage-entity-types.d.ts +0 -41
  72. package/esm/tools/manage-entity-types.js +0 -203
  73. package/esm/tools/mutations.d.ts +0 -14
  74. package/esm/tools/mutations.js +0 -235
  75. package/esm/tools/provision-blueprint.d.ts +0 -22
  76. package/esm/tools/provision-blueprint.js +0 -111
  77. package/esm/tools/provision-database.d.ts +0 -14
  78. package/esm/tools/provision-database.js +0 -262
  79. package/esm/tools/run-codegen.d.ts +0 -11
  80. package/esm/tools/run-codegen.js +0 -139
  81. package/esm/tools/templates.d.ts +0 -49
  82. package/esm/tools/templates.js +0 -337
  83. package/policy/add-policies-to-table.d.ts +0 -26
  84. package/policy/add-policies-to-table.js +0 -71
  85. package/policy/provision-helpers.d.ts +0 -38
  86. package/policy/provision-helpers.js +0 -58
  87. package/provision-database/credential.d.ts +0 -14
  88. package/provision-database/credential.js +0 -22
  89. package/provision-database/env-file.d.ts +0 -37
  90. package/provision-database/env-file.js +0 -91
  91. package/provision-database/manifest.d.ts +0 -10
  92. package/provision-database/manifest.js +0 -56
  93. package/provision-database/pg-fixups.d.ts +0 -16
  94. package/provision-database/pg-fixups.js +0 -170
  95. package/provision-database/preset-match.d.ts +0 -13
  96. package/provision-database/preset-match.js +0 -70
  97. package/provision-database/presets.d.ts +0 -4
  98. package/provision-database/presets.js +0 -17
  99. package/provision-database/request-database.d.ts +0 -61
  100. package/provision-database/request-database.js +0 -134
  101. package/provision-database/resolve.d.ts +0 -27
  102. package/provision-database/resolve.js +0 -53
  103. package/records/meta.d.ts +0 -41
  104. package/records/meta.js +0 -172
  105. package/run-codegen/barrels.d.ts +0 -3
  106. package/run-codegen/barrels.js +0 -54
  107. package/run-codegen/endpoints.d.ts +0 -8
  108. package/run-codegen/endpoints.js +0 -51
  109. package/schema-resolve.d.ts +0 -23
  110. package/schema-resolve.js +0 -75
  111. package/tool-schema.d.ts +0 -3
  112. package/tool-schema.js +0 -13
  113. package/tools/add-policies.d.ts +0 -8
  114. package/tools/add-policies.js +0 -99
  115. package/tools/add-records.d.ts +0 -14
  116. package/tools/add-records.js +0 -127
  117. package/tools/add-relation-schema.d.ts +0 -30
  118. package/tools/add-relation-schema.js +0 -62
  119. package/tools/add-relation.d.ts +0 -10
  120. package/tools/add-relation.js +0 -94
  121. package/tools/create-api-key.d.ts +0 -18
  122. package/tools/create-api-key.js +0 -222
  123. package/tools/describe-schema.d.ts +0 -18
  124. package/tools/describe-schema.js +0 -135
  125. package/tools/manage-entity-types.d.ts +0 -41
  126. package/tools/manage-entity-types.js +0 -208
  127. package/tools/mutations.d.ts +0 -14
  128. package/tools/mutations.js +0 -238
  129. package/tools/provision-blueprint.d.ts +0 -22
  130. package/tools/provision-blueprint.js +0 -114
  131. package/tools/provision-database.d.ts +0 -14
  132. package/tools/provision-database.js +0 -268
  133. package/tools/run-codegen.d.ts +0 -11
  134. package/tools/run-codegen.js +0 -143
  135. package/tools/templates.d.ts +0 -49
  136. package/tools/templates.js +0 -341
@@ -1,341 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deleteTemplateTool = exports.updateTemplateTool = exports.applyTemplateTool = exports.createTemplateTool = exports.listTemplatesTool = void 0;
4
- exports.createTemplatePreviewTables = createTemplatePreviewTables;
5
- const harness_1 = require("@agentic-kit/harness");
6
- const zod_1 = require("zod");
7
- const context_1 = require("../context");
8
- const tool_schema_1 = require("../tool-schema");
9
- // A stored blueprint field's `type` is an expanded object (`{ name: 'text' }`),
10
- // not the bare string the agent passes to provision_blueprint. Flatten it to the
11
- // type name so the renderer gets a string, never an object (which React can't
12
- // render as a child).
13
- function fieldTypeName(raw) {
14
- if (typeof raw === 'string')
15
- return raw;
16
- if (raw && typeof raw === 'object' && typeof raw.name === 'string') {
17
- return raw.name;
18
- }
19
- return '';
20
- }
21
- // A template's definition is a stored blueprint (see BlueprintDefinitionSchema):
22
- // { tables: [{ table_name, fields, policies }], relations: [{ source_table, … }] }.
23
- // Parse it into the same table shape provision_blueprint surfaces so the renderer
24
- // can reuse TableCard. Defensive throughout — it's untyped JSON from the catalog.
25
- function parseTemplateTables(definition) {
26
- if (!definition || typeof definition !== 'object')
27
- return [];
28
- const def = definition;
29
- const tables = Array.isArray(def.tables) ? def.tables : [];
30
- const relations = Array.isArray(def.relations) ? def.relations : [];
31
- return tables.map((raw) => {
32
- const table = raw;
33
- const fields = Array.isArray(table.fields) ? table.fields : [];
34
- const policies = Array.isArray(table.policies) ? table.policies : [];
35
- return {
36
- name: table.table_name ?? '',
37
- fields: fields.map((rawField) => {
38
- const field = rawField;
39
- return {
40
- name: field.name ?? '',
41
- type: fieldTypeName(field.type),
42
- isRequired: field.is_required ?? false,
43
- defaultValue: typeof field.default === 'string' ? field.default : null,
44
- };
45
- }),
46
- policies: (0, harness_1.filterInternalPolicies)(policies.map((policy) => policy.$type ?? '').filter(Boolean)),
47
- relationCount: relations.filter((relation) => relation.source_table === table.table_name).length,
48
- };
49
- });
50
- }
51
- function formatTemplatesText(templates) {
52
- if (templates.length === 0)
53
- return 'No blueprint templates available.';
54
- const lines = [`Available blueprint templates (${templates.length}):`];
55
- for (const template of templates) {
56
- const cats = template.categories.length ? ` [${template.categories.join(', ')}]` : '';
57
- lines.push(` ${template.displayName} — ${template.tables.length} tables${cats}`);
58
- for (const table of template.tables) {
59
- lines.push(` - ${table.name} (${table.fields.length} field${table.fields.length === 1 ? '' : 's'})`);
60
- }
61
- }
62
- return lines.join('\n');
63
- }
64
- // The latest project blueprint (or a named one) — the definition source for
65
- // create_template, and for the confirm preview of the tables it will template.
66
- // A blueprintName is a best-effort hint: if it matches nothing (the agent often
67
- // guesses casing/wording), fall back to the most recent blueprint so the confirm
68
- // still previews real tables and execute templates the same one it previewed.
69
- async function fetchLatestBlueprint(context, blueprintName) {
70
- const found = await context.modules.blueprint
71
- .findMany({
72
- select: { displayName: true, definition: true },
73
- where: {
74
- databaseId: { equalTo: context.databaseId },
75
- ...(blueprintName ? { displayName: { equalTo: blueprintName } } : {}),
76
- },
77
- orderBy: ['CREATED_AT_DESC'],
78
- first: 1,
79
- })
80
- .unwrap();
81
- return found.blueprints?.nodes?.[0];
82
- }
83
- async function findProjectBlueprint(context, blueprintName) {
84
- let node = await fetchLatestBlueprint(context, blueprintName);
85
- if (!node && blueprintName)
86
- node = await fetchLatestBlueprint(context, undefined);
87
- return node?.definition ? { displayName: node.displayName ?? '', definition: node.definition } : null;
88
- }
89
- // The create_template confirm preview — the source blueprint's name and its
90
- // tables, parsed into the shape the renderer's TableCard reuses. Best-effort: any
91
- // failure yields no tables rather than blocking the confirm.
92
- async function createTemplatePreviewTables(context, blueprintName) {
93
- try {
94
- const blueprint = await findProjectBlueprint(context, blueprintName);
95
- if (!blueprint)
96
- return { blueprintName: '', tables: [] };
97
- return {
98
- blueprintName: blueprint.displayName,
99
- tables: parseTemplateTables(blueprint.definition),
100
- };
101
- }
102
- catch (err) {
103
- console.warn('[db-tools] create_template preview lookup failed:', err);
104
- return { blueprintName: '', tables: [] };
105
- }
106
- }
107
- async function resolveTemplateId(client, displayName) {
108
- const result = await client.blueprintTemplate
109
- .findMany({
110
- select: { id: true, displayName: true },
111
- where: { displayName: { equalTo: displayName } },
112
- first: 1,
113
- })
114
- .unwrap();
115
- const template = result.blueprintTemplates?.nodes?.[0];
116
- if (!template?.id)
117
- throw new Error(`Template "${displayName}" not found`);
118
- return template.id;
119
- }
120
- function done(success, message) {
121
- return { content: [{ type: 'text', text: message }], details: { success, message } };
122
- }
123
- // ── list_templates ────────────────────────────────────────────────────────
124
- const ListTemplatesZod = zod_1.z.object({});
125
- const ListTemplatesSchema = (0, tool_schema_1.toolSchema)(ListTemplatesZod);
126
- exports.listTemplatesTool = {
127
- name: 'list_templates',
128
- label: 'List templates',
129
- description: 'List the blueprint templates available to provision into the project database (display name, description, categories, table count). Read-only — these are a global catalog, not the project schema.',
130
- promptSnippet: 'list_templates: list the available blueprint templates to apply. Read-only — call before apply_template.',
131
- parameters: ListTemplatesSchema,
132
- async execute(_id, _params, _signal, _onUpdate, ctx) {
133
- const resolved = await (0, context_1.resolveProjectContext)(ctx.cwd);
134
- if (!resolved.context) {
135
- return { content: [{ type: 'text', text: resolved.reason }], details: { templates: [] } };
136
- }
137
- const result = await resolved.context.modules.blueprintTemplate
138
- .findMany({
139
- select: {
140
- displayName: true,
141
- description: true,
142
- categories: true,
143
- definition: true,
144
- },
145
- orderBy: ['NAME_ASC'],
146
- })
147
- .execute();
148
- if (!result.ok) {
149
- const message = result.errors.map((e) => e.message).join('; ');
150
- return {
151
- content: [{ type: 'text', text: `Failed to read templates: ${message}` }],
152
- details: { templates: [] },
153
- };
154
- }
155
- const templates = (result.data.blueprintTemplates?.nodes ?? []).map((template) => ({
156
- displayName: template.displayName ?? '',
157
- description: template.description ?? null,
158
- categories: Array.isArray(template.categories) ? template.categories : [],
159
- tables: parseTemplateTables(template.definition),
160
- }));
161
- return {
162
- content: [{ type: 'text', text: formatTemplatesText(templates) }],
163
- details: { templates },
164
- };
165
- },
166
- };
167
- // ── create_template ───────────────────────────────────────────────────────
168
- const CreateTemplateZod = zod_1.z.object({
169
- displayName: zod_1.z.string().describe('Display name for the new blueprint template'),
170
- blueprintName: zod_1.z
171
- .string()
172
- .describe('Display name of an existing project blueprint to save as the template. Defaults to the most recently provisioned blueprint.')
173
- .optional(),
174
- description: zod_1.z.string().describe('Description shown in the template catalog').optional(),
175
- categories: zod_1.z
176
- .array(zod_1.z.string())
177
- .describe('Categories for catalog grouping and discovery')
178
- .optional(),
179
- });
180
- const CreateTemplateSchema = (0, tool_schema_1.toolSchema)(CreateTemplateZod);
181
- exports.createTemplateTool = {
182
- name: 'create_template',
183
- label: 'Create template',
184
- description: 'Save an existing project blueprint into the global template catalog as a reusable blueprint template. Defaults to the most recently provisioned blueprint; pass blueprintName to pick a specific one by display name. Gated.',
185
- promptSnippet: 'create_template: save a project blueprint to the reusable template catalog. Gated.',
186
- parameters: CreateTemplateSchema,
187
- async execute(_id, params, _signal, _onUpdate, ctx) {
188
- const resolved = await (0, context_1.resolveProjectContext)(ctx.cwd);
189
- if (!resolved.context)
190
- return done(false, resolved.reason);
191
- const { modules, ownerId } = resolved.context;
192
- if (!ownerId)
193
- return done(false, 'Not authenticated (missing OWNER_ID in .env).');
194
- try {
195
- const blueprint = await findProjectBlueprint(resolved.context, params.blueprintName);
196
- if (!blueprint) {
197
- const which = params.blueprintName ? `"${params.blueprintName}"` : 'any blueprint';
198
- return done(false, `No project blueprint found (${which}). Provision a blueprint first.`);
199
- }
200
- const base = params.displayName.toLowerCase().replace(/\s+/g, '_');
201
- await modules.blueprintTemplate
202
- .create({
203
- data: {
204
- ownerId,
205
- name: `${base}_${Date.now()}`,
206
- displayName: params.displayName,
207
- description: params.description,
208
- categories: params.categories,
209
- definition: blueprint.definition,
210
- },
211
- select: { id: true, displayName: true },
212
- })
213
- .unwrap();
214
- const message = `Created template "${params.displayName}"`;
215
- return {
216
- content: [{ type: 'text', text: message }],
217
- details: {
218
- success: true,
219
- message,
220
- displayName: params.displayName,
221
- blueprintName: blueprint.displayName,
222
- tables: parseTemplateTables(blueprint.definition),
223
- },
224
- };
225
- }
226
- catch (err) {
227
- return done(false, err instanceof Error ? err.message : 'Failed to create template');
228
- }
229
- },
230
- };
231
- // ── apply_template ────────────────────────────────────────────────────────
232
- const ApplyTemplateZod = zod_1.z.object({
233
- templateName: zod_1.z.string().describe('The display name of the blueprint template to apply'),
234
- nameOverride: zod_1.z
235
- .string()
236
- .describe('Optional name override for the created blueprint (snake_case). Defaults to template name.')
237
- .optional(),
238
- });
239
- const ApplyTemplateSchema = (0, tool_schema_1.toolSchema)(ApplyTemplateZod);
240
- exports.applyTemplateTool = {
241
- name: 'apply_template',
242
- label: 'Apply template',
243
- description: 'Apply an existing blueprint template to the project database, creating its tables. Use the template display name (from list_templates).',
244
- promptSnippet: 'apply_template: provision a saved blueprint template by display name. Gated.',
245
- parameters: ApplyTemplateSchema,
246
- async execute(_id, params, _signal, _onUpdate, ctx) {
247
- const resolved = await (0, context_1.resolveProjectContext)(ctx.cwd);
248
- if (!resolved.context)
249
- return done(false, resolved.reason);
250
- const { modules, databaseId, schemaId, ownerId } = resolved.context;
251
- if (!ownerId)
252
- return done(false, 'Not authenticated (missing OWNER_ID in .env).');
253
- try {
254
- const templateId = await resolveTemplateId(modules, params.templateName);
255
- const base = params.nameOverride ?? params.templateName.toLowerCase().replace(/\s+/g, '_');
256
- const nameOverride = `${base}_${Date.now()}`;
257
- const copyResult = await modules.mutation
258
- .copyTemplateToBlueprint({ input: { templateId, databaseId, ownerId, nameOverride } }, { select: { result: true } })
259
- .unwrap();
260
- const blueprintId = copyResult.copyTemplateToBlueprint?.result;
261
- if (!blueprintId)
262
- throw new Error('Failed to copy template');
263
- const constructResult = await modules.mutation
264
- .constructBlueprint({ input: { blueprintId, schemaId } }, { select: { result: true } })
265
- .unwrap();
266
- const refMap = constructResult.constructBlueprint?.result;
267
- if (!refMap)
268
- return done(false, `Construction failed for template "${params.templateName}"`);
269
- const createdCount = Object.keys(refMap).length;
270
- return done(true, `Applied template "${params.templateName}" — created ${createdCount} table${createdCount === 1 ? '' : 's'}`);
271
- }
272
- catch (err) {
273
- return done(false, err instanceof Error ? err.message : 'Failed to apply template');
274
- }
275
- },
276
- };
277
- // ── update_template ───────────────────────────────────────────────────────
278
- const UpdateTemplateZod = zod_1.z.object({
279
- templateName: zod_1.z.string().describe('The display name of the blueprint template to update'),
280
- patch: zod_1.z.object({
281
- displayName: zod_1.z.string().describe('New display name').optional(),
282
- description: zod_1.z.string().describe('New description').optional(),
283
- categories: zod_1.z.array(zod_1.z.string()).describe('New categories').optional(),
284
- tags: zod_1.z.array(zod_1.z.string()).describe('New tags').optional(),
285
- visibility: zod_1.z.enum(['private', 'public']).describe('New visibility').optional(),
286
- }),
287
- });
288
- const UpdateTemplateSchema = (0, tool_schema_1.toolSchema)(UpdateTemplateZod);
289
- exports.updateTemplateTool = {
290
- name: 'update_template',
291
- label: 'Update template',
292
- description: 'Update a blueprint template’s metadata (display name, description, categories, tags, visibility).',
293
- promptSnippet: 'update_template: edit a saved template’s metadata by display name. Gated.',
294
- parameters: UpdateTemplateSchema,
295
- async execute(_id, params, _signal, _onUpdate, ctx) {
296
- const resolved = await (0, context_1.resolveProjectContext)(ctx.cwd);
297
- if (!resolved.context)
298
- return done(false, resolved.reason);
299
- try {
300
- const templateId = await resolveTemplateId(resolved.context.modules, params.templateName);
301
- await resolved.context.modules.blueprintTemplate
302
- .update({
303
- where: { id: templateId },
304
- data: params.patch,
305
- select: { id: true, displayName: true },
306
- })
307
- .unwrap();
308
- return done(true, `Updated template "${params.templateName}"`);
309
- }
310
- catch (err) {
311
- return done(false, err instanceof Error ? err.message : 'Failed to update template');
312
- }
313
- },
314
- };
315
- // ── delete_template ───────────────────────────────────────────────────────
316
- const DeleteTemplateZod = zod_1.z.object({
317
- templateName: zod_1.z.string().describe('The display name of the blueprint template to delete'),
318
- });
319
- const DeleteTemplateSchema = (0, tool_schema_1.toolSchema)(DeleteTemplateZod);
320
- exports.deleteTemplateTool = {
321
- name: 'delete_template',
322
- label: 'Delete template',
323
- description: 'Permanently delete a blueprint template by its display name.',
324
- promptSnippet: 'delete_template: remove a saved template by display name. Destructive — gated.',
325
- parameters: DeleteTemplateSchema,
326
- async execute(_id, params, _signal, _onUpdate, ctx) {
327
- const resolved = await (0, context_1.resolveProjectContext)(ctx.cwd);
328
- if (!resolved.context)
329
- return done(false, resolved.reason);
330
- try {
331
- const templateId = await resolveTemplateId(resolved.context.modules, params.templateName);
332
- await resolved.context.modules.blueprintTemplate
333
- .delete({ where: { id: templateId }, select: { id: true } })
334
- .unwrap();
335
- return done(true, `Deleted template "${params.templateName}"`);
336
- }
337
- catch (err) {
338
- return done(false, err instanceof Error ? err.message : 'Failed to delete template');
339
- }
340
- },
341
- };