@agentic-kit/db-tools 0.15.0 → 0.16.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 (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 +19 -16
  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/esm/index.d.ts CHANGED
@@ -10,14 +10,14 @@ export { type AccessPolicy, AccessPolicyZod, AuthzTypeZod, type Blueprint, bluep
10
10
  export { fieldTypeToTypeName, toFieldDefault, toFieldType } from './blueprint/field-type';
11
11
  export { isInternalPolicy } from './blueprint/internal-policies';
12
12
  export { BlueprintBuildError, buildBlueprint, CONSTRUCTION_DEADLINE_MS, type ConstructionTableMap, readConstructionFailure, waitForConstruction, type WaitForConstructionOptions, } from './blueprint-construction';
13
- export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, type ContextEnvKey, type ContextSource, deriveSubdomainEndpoint, fromEnvFile, fromEnvironment, type ModulesClient, type ProjectContext, type ProjectContextFailureCode, resolveDataToken, resolveProjectContext, type ResolveResult, } from './context';
13
+ export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, type ContextEnvKey, type ContextSource, deriveSubdomainEndpoint, fromEnvFile, fromEnvironment, type ModulesClient, type ProjectContext, type ProjectContextFailureCode, readEnv, resolveDataToken, resolveProjectContext, type ResolveResult, } from './context';
14
14
  export { type DatabaseProbe, probeBoundDatabase, probeDatabase, type UnreachableCause, } from './db-probe';
15
15
  export { failureFromErrors, type FailureMeaning } from './failure';
16
16
  export { constructiveGateDeps, type ConstructiveGateResolvers } from './gate';
17
17
  export { type BackendErrorKind, classifyBackendErrors, type HttpGraphQLResponse, postGraphQL, statusAdapter, } from './graphql-http';
18
18
  export { type PostGraphQLArgs } from './graphql-http';
19
19
  export { type ActiveDataToken, configureHost, type DataAuthBroker, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, type HostAccount, type HostBackendConfig, type HostProvisionOverlay, noBackendReason, type PreviewToken, type ProjectBinding, type SecretDelivery, type StepUpRequest, type ToolsHost, } from './host';
20
- export { archiveBindingKeys, ARCHIVED_BINDING_KEYS, type EnvVars, mergeEnv, provisionEnvVars, readEnv, } from './provision-database/env-file';
20
+ export { type ArchivedEntry, type BoundDatabase, type PendingTicket, PROJECT_FILE, type ProjectFile, readProjectFile, updateProjectFile, } from './project-file';
21
21
  export { loadProvisionManifest, parseProvisionManifest, PROVISION_MANIFEST_FILE, type ProvisionManifest, } from './provision-database/manifest';
22
22
  export { allModulePresets, DEFAULT_PROVISION_PRESET, getModulePreset, type ModulePreset, type ProvisionModule, } from './provision-database/presets';
23
23
  export { provisionDatabase, type ProvisionDatabaseDetails, type ProvisionFailureCode, type ProvisionInput, } from './provision-database/provision';
@@ -26,16 +26,177 @@ export { type RecordsDetails } from './records/rows';
26
26
  export { accessOf, type ColumnDefault, type FkAction, type ModelColumn, type ModelForeignKey, type ModelPolicy, type ModelTable, type ObjectCategory, readSchemaModel, type SchemaModel, SchemaReadError, type TableAccess, } from './schema-model';
27
27
  export { accessState, asWritten, grantWords, planRule, ruleWords, type WrittenRule } from './table-access';
28
28
  export { toolSchema } from './tool-schema';
29
- export { describePolicyTypeTool, explainAccessTool, type ExplainedRule, type ExplainedTable, setAccessTool } from './tools/access';
30
- export { type AddPoliciesDetails, addPoliciesTool } from './tools/add-policies';
31
- export { addRecordsTool } from './tools/add-records';
32
- export { type AddRelationDetails, addRelationTool } from './tools/add-relation';
33
- export { type CreateApiKeyDetails, createApiKeyTool } from './tools/create-api-key';
34
- export { type DescribeSchemaDetails, describeSchemaTool } from './tools/describe-schema';
35
- export { createEntityTypeTool, deleteEntityTypeTool, type EntityTypesDetails, type EntityTypeSummary, listEntityTypesTool } from './tools/entity-types';
36
- export { createFieldTool, deleteFieldTool, deleteTableTool, type MutationDetails, retypeFieldTool, updateFieldTool, } from './tools/mutations';
37
- export { type ProvisionBlueprintDetails, provisionBlueprintTool } from './tools/provision-blueprint';
38
- export { provisionDatabaseTool, replaceDatabaseTool } from './tools/provision-database';
39
- export { type RunCodegenDetails, type RunCodegenTimings, runCodegenTool } from './tools/run-codegen';
40
- export { applyTemplateTool, type CreateTemplateDetails, createTemplatePreviewTables, createTemplateTool, deleteTemplateTool, type ListTemplatesDetails, listTemplatesTool, type TemplateDetails, updateTemplateTool, } from './tools/templates';
29
+ export { type ExplainedRule, type ExplainedTable } from './tools/access';
30
+ export { type AddPoliciesDetails } from './tools/add-policies';
31
+ export { type AddRelationDetails } from './tools/add-relation';
32
+ export { type CreateApiKeyDetails } from './tools/create-api-key';
33
+ export { type DescribeSchemaDetails } from './tools/describe-schema';
34
+ export { type EntityTypesDetails, type EntityTypeSummary } from './tools/entity-types';
35
+ export { type MutationDetails } from './tools/mutations';
36
+ export { type ProvisionBlueprintDetails } from './tools/provision-blueprint';
37
+ export { type RunCodegenDetails, type RunCodegenTimings } from './tools/run-codegen';
38
+ export { type CreateTemplateDetails, createTemplatePreviewTables, type ListTemplatesDetails, type TemplateDetails } from './tools/templates';
39
+ export declare const addPoliciesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
40
+ table_name: import("zod").ZodString;
41
+ policies: import("zod").ZodArray<import("zod").ZodObject<{
42
+ $type: import("zod").ZodString;
43
+ privileges: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
44
+ permissive: import("zod").ZodOptional<import("zod").ZodBoolean>;
45
+ policy_role: import("zod").ZodOptional<import("zod").ZodString>;
46
+ policy_name: import("zod").ZodOptional<import("zod").ZodString>;
47
+ data: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
48
+ }, import("zod/v4/core").$loose>>;
49
+ }, import("zod/v4/core").$strict>, import(".").AddPoliciesDetails>, addRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
50
+ table_name: import("zod").ZodString;
51
+ records: import("zod").ZodArray<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
52
+ }, import("zod/v4/core").$strict>, import(".").RecordsDetails>, addRelationTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
53
+ relation_type: import("zod").ZodOptional<import("zod").ZodEnum<{
54
+ belongs_to: "belongs_to";
55
+ many_to_many: "many_to_many";
56
+ }>>;
57
+ source_table: import("zod").ZodString;
58
+ target_table: import("zod").ZodString;
59
+ field_name: import("zod").ZodOptional<import("zod").ZodString>;
60
+ junction_table_name: import("zod").ZodOptional<import("zod").ZodString>;
61
+ delete_action: import("zod").ZodOptional<import("zod").ZodEnum<{
62
+ c: "c";
63
+ n: "n";
64
+ r: "r";
65
+ a: "a";
66
+ d: "d";
67
+ }>>;
68
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
69
+ }, import("zod/v4/core").$strict>, import(".").AddRelationDetails>, applyTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
70
+ templateName: import("zod").ZodString;
71
+ nameOverride: import("zod").ZodOptional<import("zod").ZodString>;
72
+ }, import("zod/v4/core").$strict>, import(".").TemplateDetails>, createApiKeyTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
73
+ key_name: import("zod").ZodString;
74
+ principal_name: import("zod").ZodOptional<import("zod").ZodString>;
75
+ entity_ids: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodGUID>>;
76
+ read_only: import("zod").ZodOptional<import("zod").ZodBoolean>;
77
+ expires_in_days: import("zod").ZodOptional<import("zod").ZodNumber>;
78
+ }, import("zod/v4/core").$strict>, import(".").CreateApiKeyDetails>, createEntityTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
79
+ name: import("zod").ZodString;
80
+ description: import("zod").ZodOptional<import("zod").ZodString>;
81
+ prefix: import("zod").ZodOptional<import("zod").ZodString>;
82
+ parent_entity: import("zod").ZodOptional<import("zod").ZodString>;
83
+ is_visible: import("zod").ZodOptional<import("zod").ZodBoolean>;
84
+ has_invites: import("zod").ZodOptional<import("zod").ZodBoolean>;
85
+ has_profiles: import("zod").ZodOptional<import("zod").ZodBoolean>;
86
+ has_limits: import("zod").ZodOptional<import("zod").ZodBoolean>;
87
+ has_levels: import("zod").ZodOptional<import("zod").ZodBoolean>;
88
+ has_storage: import("zod").ZodOptional<import("zod").ZodBoolean>;
89
+ }, import("zod/v4/core").$strict>, import(".").EntityTypesDetails>, createFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
90
+ table_name: import("zod").ZodString;
91
+ field_name: import("zod").ZodString;
92
+ type: import("zod").ZodString;
93
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
94
+ default_value: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodBoolean, import("zod").ZodNull]>>;
95
+ is_unique: import("zod").ZodOptional<import("zod").ZodBoolean>;
96
+ }, import("zod/v4/core").$strict>, import(".").MutationDetails>, 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(".").CreateTemplateDetails>, deleteEntityTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
102
+ entity_type_id: import("zod").ZodGUID;
103
+ }, import("zod/v4/core").$strict>, import(".").EntityTypesDetails>, deleteFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
104
+ table_name: import("zod").ZodString;
105
+ field_name: import("zod").ZodString;
106
+ }, import("zod/v4/core").$strict>, import(".").MutationDetails>, deleteRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
107
+ tables: import("zod").ZodArray<import("zod").ZodObject<{
108
+ table_name: import("zod").ZodString;
109
+ rows: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber]>>>;
110
+ where: import("zod").ZodOptional<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
111
+ sample: import("zod").ZodOptional<import("zod").ZodLiteral<true>>;
112
+ all: import("zod").ZodOptional<import("zod").ZodLiteral<true>>;
113
+ }, import("zod/v4/core").$strict>>;
114
+ }, import("zod/v4/core").$strict>, import("./tools/delete-records").DeleteRecordsDetails>, deleteTableTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
115
+ table_name: import("zod").ZodString;
116
+ }, import("zod/v4/core").$strict>, import(".").MutationDetails>, deleteTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
117
+ templateName: import("zod").ZodString;
118
+ }, import("zod/v4/core").$strict>, import(".").TemplateDetails>, describePolicyTypeTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
119
+ type: import("zod").ZodOptional<import("zod").ZodEnum<{
120
+ [x: string]: string;
121
+ }>>;
122
+ }, import("zod/v4/core").$strict>, {
123
+ types?: {
124
+ name: string;
125
+ summary?: string;
126
+ tableKind?: string;
127
+ }[];
128
+ type?: {
129
+ name: string;
130
+ summary?: string;
131
+ tableKind?: string;
132
+ } & {
133
+ parameterSchema: unknown;
134
+ useWhen: string[];
135
+ avoidWhen: string[];
136
+ risks: string[];
137
+ dataNode?: unknown;
138
+ };
139
+ }>, describeSchemaTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import(".").DescribeSchemaDetails>, explainAccessTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
140
+ table_name: import("zod").ZodOptional<import("zod").ZodString>;
141
+ }, import("zod/v4/core").$strict>, {
142
+ tables: import(".").ExplainedTable[];
143
+ }>, listEntityTypesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import(".").EntityTypesDetails>, listTemplatesTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import(".").ListTemplatesDetails>, provisionBlueprintTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
144
+ name: import("zod").ZodString;
145
+ description: import("zod").ZodString;
146
+ definition: import("zod").ZodObject<{
147
+ tables: import("zod").ZodArray<import("zod").ZodObject<{
148
+ table_name: import("zod").ZodOptional<import("zod").ZodString>;
149
+ fields: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodObject<{
150
+ name: import("zod").ZodString;
151
+ }, import("zod/v4/core").$loose>>>;
152
+ }, import("zod/v4/core").$loose>>;
153
+ }, import("zod/v4/core").$loose>;
154
+ categories: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
155
+ tags: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
156
+ }, import("zod/v4/core").$strict>, import(".").ProvisionBlueprintDetails>, provisionDatabaseTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
157
+ database_name: import("zod").ZodString;
158
+ }, import("zod/v4/core").$strict>, import(".").ProvisionDatabaseDetails>, replaceDatabaseTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
159
+ database_name: import("zod").ZodString;
160
+ }, import("zod/v4/core").$strict>, import(".").ProvisionDatabaseDetails>, retypeFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
161
+ table_name: import("zod").ZodString;
162
+ field_name: import("zod").ZodString;
163
+ type: import("zod").ZodString;
164
+ }, import("zod/v4/core").$strict>, import(".").MutationDetails>, runCodegenTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{}, import("zod/v4/core").$strict>, import(".").RunCodegenDetails>, 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(".").ExplainedTable;
179
+ }>, updateFieldTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
180
+ table_name: import("zod").ZodString;
181
+ field_name: import("zod").ZodString;
182
+ new_name: import("zod").ZodOptional<import("zod").ZodString>;
183
+ is_required: import("zod").ZodOptional<import("zod").ZodBoolean>;
184
+ default_value: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodString, import("zod").ZodNumber, import("zod").ZodBoolean, import("zod").ZodNull]>>;
185
+ is_unique: import("zod").ZodOptional<import("zod").ZodBoolean>;
186
+ }, import("zod/v4/core").$strict>, import(".").MutationDetails>, updateRecordsTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
187
+ table_name: import("zod").ZodString;
188
+ records: import("zod").ZodArray<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodUnknown>>;
189
+ }, import("zod/v4/core").$strict>, import(".").RecordsDetails>, updateTemplateTool: import("@agentic-kit/harness").HarnessTool<import("zod").ZodObject<{
190
+ templateName: import("zod").ZodString;
191
+ patch: import("zod").ZodObject<{
192
+ displayName: import("zod").ZodOptional<import("zod").ZodString>;
193
+ description: import("zod").ZodOptional<import("zod").ZodString>;
194
+ categories: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
195
+ tags: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodString>>;
196
+ visibility: import("zod").ZodOptional<import("zod").ZodEnum<{
197
+ public: "public";
198
+ private: "private";
199
+ }>>;
200
+ }, import("zod/v4/core").$strict>;
201
+ }, import("zod/v4/core").$strict>, import(".").TemplateDetails>;
41
202
  export default constructiveDbTools;
package/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { configureHost } from './host';
2
- import { constructiveDbTools } from './tools';
2
+ import { constructiveDbTools, servedTools } from './tools';
3
3
  export { constructiveDbTools };
4
4
  /** Configure the host and get the tools in one call. */
5
5
  export function createConstructiveDbTools(host) {
@@ -12,13 +12,13 @@ export { AccessPolicyZod, AuthzTypeZod, blueprintProblems, BlueprintZod, } from
12
12
  export { fieldTypeToTypeName, toFieldDefault, toFieldType } from './blueprint/field-type';
13
13
  export { isInternalPolicy } from './blueprint/internal-policies';
14
14
  export { BlueprintBuildError, buildBlueprint, CONSTRUCTION_DEADLINE_MS, readConstructionFailure, waitForConstruction, } from './blueprint-construction';
15
- export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, deriveSubdomainEndpoint, fromEnvFile, fromEnvironment, resolveDataToken, resolveProjectContext, } from './context';
15
+ export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, deriveSubdomainEndpoint, fromEnvFile, fromEnvironment, readEnv, resolveDataToken, resolveProjectContext, } from './context';
16
16
  export { probeBoundDatabase, probeDatabase, } from './db-probe';
17
17
  export { failureFromErrors } from './failure';
18
18
  export { constructiveGateDeps } from './gate';
19
19
  export { classifyBackendErrors, postGraphQL, statusAdapter, } from './graphql-http';
20
20
  export { configureHost, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, noBackendReason, } from './host';
21
- export { archiveBindingKeys, ARCHIVED_BINDING_KEYS, mergeEnv, provisionEnvVars, readEnv, } from './provision-database/env-file';
21
+ export { PROJECT_FILE, readProjectFile, updateProjectFile, } from './project-file';
22
22
  export { loadProvisionManifest, parseProvisionManifest, PROVISION_MANIFEST_FILE, } from './provision-database/manifest';
23
23
  export { allModulePresets, DEFAULT_PROVISION_PRESET, getModulePreset, } from './provision-database/presets';
24
24
  export { provisionDatabase, } from './provision-database/provision';
@@ -26,16 +26,6 @@ export { moduleKey, resolveProvisionModules, } from './provision-database/resolv
26
26
  export { accessOf, readSchemaModel, SchemaReadError, } from './schema-model';
27
27
  export { accessState, asWritten, grantWords, planRule, ruleWords } from './table-access';
28
28
  export { toolSchema } from './tool-schema';
29
- export { describePolicyTypeTool, explainAccessTool, setAccessTool } from './tools/access';
30
- export { addPoliciesTool } from './tools/add-policies';
31
- export { addRecordsTool } from './tools/add-records';
32
- export { addRelationTool } from './tools/add-relation';
33
- export { createApiKeyTool } from './tools/create-api-key';
34
- export { describeSchemaTool } from './tools/describe-schema';
35
- export { createEntityTypeTool, deleteEntityTypeTool, listEntityTypesTool } from './tools/entity-types';
36
- export { createFieldTool, deleteFieldTool, deleteTableTool, retypeFieldTool, updateFieldTool, } from './tools/mutations';
37
- export { provisionBlueprintTool } from './tools/provision-blueprint';
38
- export { provisionDatabaseTool, replaceDatabaseTool } from './tools/provision-database';
39
- export { runCodegenTool } from './tools/run-codegen';
40
- export { applyTemplateTool, createTemplatePreviewTables, createTemplateTool, deleteTemplateTool, listTemplatesTool, updateTemplateTool, } from './tools/templates';
29
+ export { createTemplatePreviewTables } from './tools/templates';
30
+ export const { addPoliciesTool, addRecordsTool, addRelationTool, applyTemplateTool, createApiKeyTool, createEntityTypeTool, createFieldTool, createTemplateTool, deleteEntityTypeTool, deleteFieldTool, deleteRecordsTool, deleteTableTool, deleteTemplateTool, describePolicyTypeTool, describeSchemaTool, explainAccessTool, listEntityTypesTool, listTemplatesTool, provisionBlueprintTool, provisionDatabaseTool, replaceDatabaseTool, retypeFieldTool, runCodegenTool, setAccessTool, updateFieldTool, updateRecordsTool, updateTemplateTool } = servedTools;
41
31
  export default constructiveDbTools;
@@ -0,0 +1,72 @@
1
+ import type { Result } from './failure';
2
+ /** The binding's file, relative to the project directory. */
3
+ export declare const PROJECT_FILE: string;
4
+ /** The bound database. A host that binds a work directory of its own may know only its id. */
5
+ export type BoundDatabase = {
6
+ id: string;
7
+ name?: string;
8
+ ownerId?: string;
9
+ };
10
+ /**
11
+ * The provision request provision_database stopped waiting on: the next call with the same name resumes it. Only a
12
+ * ticket of this backend and account is followed.
13
+ */
14
+ export type PendingTicket = {
15
+ ticketId: string;
16
+ databaseName: string;
17
+ apiEndpoint: string;
18
+ userId: string;
19
+ /** Pending results recorded for this ticket so far. */
20
+ attempts: number;
21
+ };
22
+ /** An earlier binding or request, kept readable when a new one replaced it. */
23
+ export type ArchivedEntry = {
24
+ archivedOn: string;
25
+ backend?: string;
26
+ database?: BoundDatabase;
27
+ pending?: PendingTicket;
28
+ };
29
+ export type ProjectFile = {
30
+ /** The api endpoint of the backend the database is on. It is compared with the session's backend, never sent to. */
31
+ backend?: string;
32
+ database?: BoundDatabase;
33
+ /** The agent `setup agent` set this project up for. A command still acts as the person unless it says which agent. */
34
+ agent?: string;
35
+ pending?: PendingTicket;
36
+ /** Tickets the host told provisioning to stop following: a lookup by name skips them. */
37
+ abandoned?: string[];
38
+ archived?: ArchivedEntry[];
39
+ };
40
+ /** The backend's own check on a database name, which is 3 to 63 characters long. */
41
+ export declare const DATABASE_NAME_PATTERN: RegExp;
42
+ export declare const isDatabaseName: (value: unknown) => value is string;
43
+ export declare const isUuid: (value: unknown) => value is string;
44
+ export declare const isEndpoint: (value: unknown) => value is string;
45
+ /**
46
+ * The binding of `cwd`: null when the directory has none, a failure when the file is not JSON or a value is not of its
47
+ * form. A cloned project's file is not the person's text, so a value that fails its check is never used, sent or shown.
48
+ */
49
+ export declare function readProjectFile(cwd: string): Promise<Result<ProjectFile | null>>;
50
+ /**
51
+ * Change the binding of `cwd` from its current content, read right before the write, so a change another tool or the
52
+ * person made while this call waited on the backend is kept. The write replaces the file in one step. The first write
53
+ * also tells git to leave the file out. A file that can't be read is left as it is, and the edit throws, as it does
54
+ * when .constructive links outside the project.
55
+ */
56
+ export declare function updateProjectFile(cwd: string, edit: (file: ProjectFile) => ProjectFile): Promise<void>;
57
+ export declare const recordPendingTicket: (file: ProjectFile, ticket: PendingTicket) => ProjectFile;
58
+ export declare const clearPendingTicket: (file: ProjectFile) => ProjectFile;
59
+ /** Keep the recorded request readable in the archive, and record no request. */
60
+ export declare const archivePendingTicket: (file: ProjectFile, archivedOn: string) => ProjectFile;
61
+ export declare const abandonedTickets: (file: ProjectFile) => Set<string>;
62
+ /** Add ticket ids to the abandoned list. */
63
+ export declare const recordAbandoned: (file: ProjectFile, ticketIds: readonly string[]) => ProjectFile;
64
+ /**
65
+ * Bind the directory to `database` on `backend`, with no recorded request. A binding it replaces is kept readable in
66
+ * the archive.
67
+ */
68
+ export declare function bindProject(file: ProjectFile, binding: {
69
+ database: BoundDatabase;
70
+ backend: string;
71
+ archivedOn: string;
72
+ }): ProjectFile;
@@ -0,0 +1,161 @@
1
+ // A project's binding: which database a directory works on, kept in <dir>/.constructive/project.json. Only the tools
2
+ // write it. It names no endpoint and holds no secret, so the file of a cloned project can't send a token anywhere.
3
+ import { randomBytes } from 'node:crypto';
4
+ import { mkdir, readFile, realpath, rename, rm, writeFile } from 'node:fs/promises';
5
+ import path from 'node:path';
6
+ /** The binding's file, relative to the project directory. */
7
+ export const PROJECT_FILE = path.join('.constructive', 'project.json');
8
+ /** The binding is the person's own, for their account and backend, so git leaves it out (other .constructive files stay). */
9
+ const GITIGNORE = path.join('.constructive', '.gitignore');
10
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
11
+ /** The backend's own check on a database name, which is 3 to 63 characters long. */
12
+ export const DATABASE_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
13
+ export const isDatabaseName = (value) => typeof value === 'string' && value.length >= 3 && value.length <= 63 && DATABASE_NAME_PATTERN.test(value);
14
+ export const isUuid = (value) => typeof value === 'string' && UUID.test(value);
15
+ export const isEndpoint = (value) => {
16
+ if (typeof value !== 'string')
17
+ return false;
18
+ try {
19
+ return ['http:', 'https:'].includes(new URL(value).protocol);
20
+ }
21
+ catch {
22
+ return false;
23
+ }
24
+ };
25
+ const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
26
+ /** The keys of a parsed file whose value is not of its form; such a file is never used, and its values never shown. */
27
+ function invalidKeys(file) {
28
+ const invalid = [];
29
+ const check = (key, ok) => {
30
+ if (!ok)
31
+ invalid.push(key);
32
+ };
33
+ if (file.backend !== undefined)
34
+ check('backend', isEndpoint(file.backend));
35
+ if (file.database !== undefined) {
36
+ const database = file.database;
37
+ check('database', isObject(database));
38
+ if (isObject(database)) {
39
+ check('database.id', isUuid(database.id));
40
+ if (database.name !== undefined)
41
+ check('database.name', isDatabaseName(database.name));
42
+ if (database.ownerId !== undefined)
43
+ check('database.ownerId', isUuid(database.ownerId));
44
+ }
45
+ }
46
+ if (file.agent !== undefined)
47
+ check('agent', typeof file.agent === 'string');
48
+ if (file.pending !== undefined) {
49
+ const pending = file.pending;
50
+ check('pending', isObject(pending));
51
+ if (isObject(pending)) {
52
+ check('pending.ticketId', isUuid(pending.ticketId));
53
+ check('pending.databaseName', isDatabaseName(pending.databaseName));
54
+ check('pending.apiEndpoint', isEndpoint(pending.apiEndpoint));
55
+ check('pending.userId', isUuid(pending.userId));
56
+ check('pending.attempts', Number.isInteger(pending.attempts) && pending.attempts >= 0);
57
+ }
58
+ }
59
+ if (file.abandoned !== undefined)
60
+ check('abandoned', Array.isArray(file.abandoned) && file.abandoned.every(isUuid));
61
+ if (file.archived !== undefined)
62
+ check('archived', Array.isArray(file.archived) && file.archived.every(isObject));
63
+ return invalid;
64
+ }
65
+ /**
66
+ * The binding of `cwd`: null when the directory has none, a failure when the file is not JSON or a value is not of its
67
+ * form. A cloned project's file is not the person's text, so a value that fails its check is never used, sent or shown.
68
+ */
69
+ export async function readProjectFile(cwd) {
70
+ let text;
71
+ try {
72
+ text = await readFile(path.join(cwd, PROJECT_FILE), 'utf8');
73
+ }
74
+ catch (err) {
75
+ if (err.code === 'ENOENT')
76
+ return { kind: 'ok', value: null };
77
+ return { kind: 'failed', failure: { code: 'missing-credentials', message: `${PROJECT_FILE} could not be read (${err.message}).` } };
78
+ }
79
+ let parsed;
80
+ try {
81
+ parsed = JSON.parse(text);
82
+ }
83
+ catch {
84
+ parsed = undefined;
85
+ }
86
+ if (!isObject(parsed)) {
87
+ return {
88
+ kind: 'failed',
89
+ failure: {
90
+ code: 'missing-credentials',
91
+ message: `${PROJECT_FILE} is not a JSON object, so this directory's binding can't be read.`,
92
+ hint: 'Remove the file and bind the directory again (the person approves it).',
93
+ },
94
+ };
95
+ }
96
+ const invalid = invalidKeys(parsed);
97
+ if (invalid.length) {
98
+ return {
99
+ kind: 'failed',
100
+ failure: {
101
+ code: 'missing-credentials',
102
+ message: `${PROJECT_FILE} has values that are not of their form (${invalid.join(', ')}), so none of it is used.`,
103
+ hint: 'Remove the file and bind the directory again (the person approves it).',
104
+ },
105
+ };
106
+ }
107
+ return { kind: 'ok', value: parsed };
108
+ }
109
+ /**
110
+ * Change the binding of `cwd` from its current content, read right before the write, so a change another tool or the
111
+ * person made while this call waited on the backend is kept. The write replaces the file in one step. The first write
112
+ * also tells git to leave the file out. A file that can't be read is left as it is, and the edit throws, as it does
113
+ * when .constructive links outside the project.
114
+ */
115
+ export async function updateProjectFile(cwd, edit) {
116
+ const current = await readProjectFile(cwd);
117
+ if (current.kind === 'failed')
118
+ throw new Error(current.failure.message);
119
+ const file = path.join(cwd, PROJECT_FILE);
120
+ await mkdir(path.dirname(file), { recursive: true });
121
+ const inside = path.relative(await realpath(cwd), await realpath(path.dirname(file)));
122
+ if (inside.startsWith('..') || path.isAbsolute(inside))
123
+ throw new Error(`${PROJECT_FILE} was not written: .constructive links outside the project.`);
124
+ if (current.value === null)
125
+ await writeFile(path.join(cwd, GITIGNORE), 'project.json\n', { flag: 'wx' }).catch(() => undefined);
126
+ const temporary = `${file}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
127
+ try {
128
+ await writeFile(temporary, `${JSON.stringify(edit(current.value ?? {}), null, 2)}\n`, { flag: 'wx' });
129
+ await rename(temporary, file);
130
+ }
131
+ catch (err) {
132
+ await rm(temporary, { force: true });
133
+ throw err;
134
+ }
135
+ }
136
+ /** The newest abandoned ids kept; older ones are long finished or failed. */
137
+ const MAX_ABANDONED = 10;
138
+ const without = (file, key) => {
139
+ const rest = { ...file };
140
+ delete rest[key];
141
+ return rest;
142
+ };
143
+ export const recordPendingTicket = (file, ticket) => ({ ...file, pending: ticket });
144
+ export const clearPendingTicket = (file) => without(file, 'pending');
145
+ /** Keep the recorded request readable in the archive, and record no request. */
146
+ export const archivePendingTicket = (file, archivedOn) => file.pending ? { ...without(file, 'pending'), archived: [...(file.archived ?? []), { archivedOn, pending: file.pending }] } : file;
147
+ export const abandonedTickets = (file) => new Set(file.abandoned ?? []);
148
+ /** Add ticket ids to the abandoned list. */
149
+ export const recordAbandoned = (file, ticketIds) => ({
150
+ ...file,
151
+ abandoned: [...new Set([...(file.abandoned ?? []), ...ticketIds])].slice(-MAX_ABANDONED),
152
+ });
153
+ /**
154
+ * Bind the directory to `database` on `backend`, with no recorded request. A binding it replaces is kept readable in
155
+ * the archive.
156
+ */
157
+ export function bindProject(file, binding) {
158
+ const replaced = file.database && file.database.id !== binding.database.id;
159
+ const archived = replaced ? [...(file.archived ?? []), { archivedOn: binding.archivedOn, backend: file.backend, database: file.database }] : file.archived;
160
+ return { ...without(file, 'pending'), backend: binding.backend, database: binding.database, ...(archived ? { archived } : {}) };
161
+ }
@@ -18,7 +18,7 @@ export type ProvisionInput = {
18
18
  */
19
19
  adopt?: string;
20
20
  };
21
- export type ProvisionFailureCode = 'signed-out' | 'backend-unreachable' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'binding-live' | 'binding-changed' | 'pending-other' | 'ticket-lost' | 'provision-stuck' | 'name-taken' | 'binding-not-saved' | 'env-write-failed' | 'reprovision-not-allowed' | 'no-backend' | 'invalid-input' | 'missing-credentials' | 'plan-required' | 'limit-reached' | 'failed';
21
+ export type ProvisionFailureCode = 'signed-out' | 'backend-unreachable' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'binding-live' | 'binding-changed' | 'pending-other' | 'ticket-lost' | 'provision-stuck' | 'name-taken' | 'binding-not-saved' | 'binding-write-failed' | 'reprovision-not-allowed' | 'no-backend' | 'invalid-input' | 'missing-credentials' | 'plan-required' | 'limit-reached' | 'failed';
22
22
  export type ProvisionDatabaseDetails = {
23
23
  success: boolean;
24
24
  message: string;
@@ -38,7 +38,7 @@ export type ProvisionDatabaseDetails = {
38
38
  ticketId?: string;
39
39
  /** True when this call finished a provision an earlier call had started. */
40
40
  resumed?: boolean;
41
- /** True when that provision was found on the backend by name, not in `.env`. */
41
+ /** True when that provision was found on the backend by name, not in the project file. */
42
42
  recovered?: boolean;
43
43
  /** The database the new binding replaced (replace mode). */
44
44
  replaced?: string;
@@ -55,7 +55,12 @@ export type ProvisionDatabaseDetails = {
55
55
  appWorkspacePrepared?: boolean;
56
56
  };
57
57
  type Details = ProvisionDatabaseDetails;
58
- /** The DATABASE_ID in a project's `.env`, if it has one of the form a database id takes. */
58
+ /**
59
+ * Pending results recorded for one ticket before provision_database stops
60
+ * following it. At the default 240 s deadline that is about 20 minutes.
61
+ */
62
+ export declare const MAX_PENDING_ATTEMPTS = 5;
63
+ /** The id of the database `cwd` is bound to, if its binding can be read. */
59
64
  export declare function boundDatabaseId(cwd: string): Promise<string | undefined>;
60
65
  /**
61
66
  * What provision_database and replace_database check before they change