@jskit-ai/crud-core 0.2.8 → 0.2.9
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/CHANGELOG.md +8 -0
- package/package.json +6 -6
- package/patterns/json-api-resource-package/PATTERN.md +11 -0
- package/patterns/json-api-resource-package/example/packages/books/package.json +2 -2
- package/src/server/defineCrudJsonApiFeature.js +5 -3
- package/src/server/jsonApiModule/actions.js +40 -20
- package/test/support/crudJsonApiActionOptions.js +1 -0
- package/test/unservedCrudAutomation.test.js +99 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
- The actions of an `internal: true` or `routes: false` CRUD feature are left off the `automation` channel, so the assistant cannot call them, unless their permission names a permission (`require: "all"` or `"any"`), as workspace-scoped CRUD's defaults do. An additional action's own `channels` still decide for it.
|
|
6
|
+
|
|
7
|
+
### Upgrading
|
|
8
|
+
|
|
9
|
+
- Give an internal resource explicit `permissions` when the assistant should keep reading it for the users who hold them.
|
|
10
|
+
|
|
3
11
|
## 0.2.0 - 2026-10-29
|
|
4
12
|
|
|
5
13
|
- Translation ready: every user-visible text is a message key with its English in `locales/en.json`, so an app translates it by supplying translation files only. With no translations, texts and behaviour are unchanged. See the guide "Translating a JSKIT app".
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/crud-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24.0.0"
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"./locales/en.json": "./locales/en.json"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
31
|
-
"@jskit-ai/json-rest-api-core": "0.2.
|
|
32
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
30
|
+
"@jskit-ai/database-runtime": "0.1.257",
|
|
31
|
+
"@jskit-ai/json-rest-api-core": "0.2.9",
|
|
32
|
+
"@jskit-ai/resource-crud-core": "0.1.199",
|
|
33
33
|
"json-rest-schema": "^1.0.17",
|
|
34
34
|
"json-rest-api": "2.1.0"
|
|
35
35
|
},
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
54
|
-
"@jskit-ai/kernel": "0.2.
|
|
53
|
+
"@jskit-ai/http-runtime": "0.2.8",
|
|
54
|
+
"@jskit-ai/kernel": "0.2.8"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -120,6 +120,17 @@ surface further; do not register unused mutation actions and routes. Use
|
|
|
120
120
|
`routes: false` only for an internal
|
|
121
121
|
capability/action surface that deliberately has no HTTP API.
|
|
122
122
|
|
|
123
|
+
The assistant runs actions on the `automation` channel as the signed-in user,
|
|
124
|
+
so an action without an HTTP route would otherwise reach records that no route
|
|
125
|
+
shows the user. The actions of an `internal: true` or `routes: false` feature
|
|
126
|
+
therefore stay off `automation` (the assistant cannot call them) unless their
|
|
127
|
+
permission names a permission (`require: "all"` or `"any"`), as the workspace
|
|
128
|
+
scope's default `crud.<namespace>.<operation>` permissions do. Give such a
|
|
129
|
+
feature explicit `permissions` when the assistant should read it for the users
|
|
130
|
+
who hold them; the default for other surfaces, `require: "authenticated"`,
|
|
131
|
+
would admit every signed-in user. An additional action's own `channels`
|
|
132
|
+
decide for it.
|
|
133
|
+
|
|
123
134
|
Create, update, and delete publish `<namespace>.record.changed` to the
|
|
124
135
|
`event_scope` realtime audience, which reaches only the room of the action's
|
|
125
136
|
user, workspace, or workspace-user scope. Set `realtimeAudience` to another
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"./shared": "./src/shared/index.js"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@jskit-ai/crud-core": "0.2.
|
|
13
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
12
|
+
"@jskit-ai/crud-core": "0.2.9",
|
|
13
|
+
"@jskit-ai/resource-crud-core": "0.1.199"
|
|
14
14
|
},
|
|
15
15
|
"jskit": {
|
|
16
16
|
"kind": "runtime",
|
|
@@ -160,8 +160,9 @@ function projectEnabledServiceOperations(service, operations) {
|
|
|
160
160
|
* @param {string} [feature.capability] Defaults to the feature id.
|
|
161
161
|
* @param {string} [feature.ownershipFilter] Defaults to the resource's autofilter, then "public".
|
|
162
162
|
* @param {string} [feature.relativePath] Defaults to the namespace, with dashes.
|
|
163
|
-
* @param {boolean} [feature.internal]
|
|
164
|
-
* @param {boolean} [feature.routes] False registers no HTTP routes.
|
|
163
|
+
* @param {boolean} [feature.internal] True keeps the routes off the HTTP server.
|
|
164
|
+
* @param {boolean} [feature.routes] False registers no HTTP routes. Either way, an action no permission
|
|
165
|
+
* guards is then left off the automation channel, so the assistant cannot run it.
|
|
165
166
|
* @param {object | null} [feature.listFilterQueryValidator]
|
|
166
167
|
* @param {object | null} [feature.searchSchema]
|
|
167
168
|
* @param {object | null} [feature.permissions]
|
|
@@ -335,7 +336,8 @@ function defineCrudJsonApiFeature({
|
|
|
335
336
|
}),
|
|
336
337
|
scopeInputValidator: normalizedScope.actionInputValidator,
|
|
337
338
|
scopeInputKeys: normalizedScope.inputKeys,
|
|
338
|
-
realtimeAudience
|
|
339
|
+
realtimeAudience,
|
|
340
|
+
servedOverHttp: routes && !internal
|
|
339
341
|
});
|
|
340
342
|
}
|
|
341
343
|
});
|
|
@@ -18,6 +18,19 @@ import { buildCrudCustomActionId } from "./customActionId.js";
|
|
|
18
18
|
const CRUD_OPERATION_NAMES = Object.freeze(["list", "view", "create", "update", "delete"]);
|
|
19
19
|
const CRUD_MUTATION_NAMES = new Set(["create", "update", "delete"]);
|
|
20
20
|
const CRUD_LIFECYCLE_PHASES = Object.freeze(["before", "execute", "after", "afterCommit"]);
|
|
21
|
+
const SERVED_ACTION_CHANNELS = Object.freeze(["api", "automation", "internal"]);
|
|
22
|
+
const UNSERVED_ACTION_CHANNELS = Object.freeze(["api", "internal"]);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A CRUD action's channels. An action that no HTTP route serves and no permission guards stays
|
|
26
|
+
* off the automation channel, so an assistant cannot reach records its user cannot reach over HTTP.
|
|
27
|
+
* @param {{ require: string, permissions?: readonly string[] }} permission
|
|
28
|
+
* @param {boolean} servedOverHttp
|
|
29
|
+
*/
|
|
30
|
+
function resolveCrudActionChannels(permission, servedOverHttp) {
|
|
31
|
+
const guarded = ["all", "any"].includes(permission.require) && Array.isArray(permission.permissions) && permission.permissions.length > 0;
|
|
32
|
+
return servedOverHttp || guarded ? SERVED_ACTION_CHANNELS : UNSERVED_ACTION_CHANNELS;
|
|
33
|
+
}
|
|
21
34
|
|
|
22
35
|
function buildActionInput(resource, operation, scopeInputValidator, listFilterQueryValidator, operationInputs) {
|
|
23
36
|
const definitions = scopeInputValidator ? [scopeInputValidator] : [];
|
|
@@ -98,7 +111,7 @@ function buildLifecycleContext(value) {
|
|
|
98
111
|
});
|
|
99
112
|
}
|
|
100
113
|
|
|
101
|
-
function normalizeAdditionalActions(definitions, { namespace, surface, permissionForAction, scopeInputValidator }) {
|
|
114
|
+
function normalizeAdditionalActions(definitions, { namespace, surface, permissionForAction, scopeInputValidator, servedOverHttp }) {
|
|
102
115
|
return Object.freeze(Object.entries(definitions).map(([name, definition]) => {
|
|
103
116
|
const actionName = String(name || "").trim();
|
|
104
117
|
if (!ARCHITECTURE_ID_PATTERN.test(actionName)) {
|
|
@@ -122,15 +135,16 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
122
135
|
}
|
|
123
136
|
|
|
124
137
|
const kind = definition.kind || "command";
|
|
138
|
+
const permission = definition.permission || permissionForAction(actionName);
|
|
125
139
|
return Object.freeze({
|
|
126
140
|
...definition,
|
|
127
141
|
name: actionName,
|
|
128
142
|
id: buildCrudCustomActionId(namespace, actionName, definition),
|
|
129
143
|
version: definition.version || 1,
|
|
130
144
|
kind,
|
|
131
|
-
channels: definition.channels ||
|
|
145
|
+
channels: definition.channels || resolveCrudActionChannels(permission, servedOverHttp),
|
|
132
146
|
surfaces: definition.surfaces || [surface],
|
|
133
|
-
permission
|
|
147
|
+
permission,
|
|
134
148
|
input: scopeInputValidator
|
|
135
149
|
? composeSchemaDefinitions([scopeInputValidator, definition.input], {
|
|
136
150
|
mode: definition.input.mode || ACTION_INPUT_DEFAULT_MODE
|
|
@@ -160,6 +174,7 @@ function normalizeAdditionalActions(definitions, { namespace, surface, permissio
|
|
|
160
174
|
* @param {Record<string, object>} dependencies.actions The feature's custom actions, by name.
|
|
161
175
|
* @param {Record<string, unknown>} dependencies.dependencies The capabilities in the feature's `requires`, by local name.
|
|
162
176
|
* @param {string} dependencies.realtimeAudience
|
|
177
|
+
* @param {boolean} dependencies.servedOverHttp False when the feature is internal or registers no routes.
|
|
163
178
|
*/
|
|
164
179
|
function createCrudJsonApiActions({
|
|
165
180
|
namespace,
|
|
@@ -176,7 +191,8 @@ function createCrudJsonApiActions({
|
|
|
176
191
|
operationInputs,
|
|
177
192
|
actions,
|
|
178
193
|
dependencies,
|
|
179
|
-
realtimeAudience
|
|
194
|
+
realtimeAudience,
|
|
195
|
+
servedOverHttp
|
|
180
196
|
}) {
|
|
181
197
|
const mutationEvent = (operation, entityId) => buildCrudRecordChangedEvent({
|
|
182
198
|
namespace,
|
|
@@ -188,7 +204,8 @@ function createCrudJsonApiActions({
|
|
|
188
204
|
namespace,
|
|
189
205
|
surface,
|
|
190
206
|
permissionForAction: permissionForOperation,
|
|
191
|
-
scopeInputValidator
|
|
207
|
+
scopeInputValidator,
|
|
208
|
+
servedOverHttp
|
|
192
209
|
});
|
|
193
210
|
|
|
194
211
|
function listDocuments(input, context) {
|
|
@@ -315,21 +332,24 @@ function createCrudJsonApiActions({
|
|
|
315
332
|
];
|
|
316
333
|
|
|
317
334
|
const enabledOperations = new Set(operations);
|
|
318
|
-
const standardActions = standardDefinitions.filter((definition) => enabledOperations.has(definition.operation)).map((definition) =>
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
335
|
+
const standardActions = standardDefinitions.filter((definition) => enabledOperations.has(definition.operation)).map((definition) => {
|
|
336
|
+
const permission = permissionForOperation(definition.operation);
|
|
337
|
+
return Object.freeze({
|
|
338
|
+
id: `crud.${namespace}.${definition.operation}`,
|
|
339
|
+
version: 1,
|
|
340
|
+
kind: definition.kind,
|
|
341
|
+
channels: resolveCrudActionChannels(permission, servedOverHttp),
|
|
342
|
+
surfaces: [surface],
|
|
343
|
+
permission,
|
|
344
|
+
input: buildActionInput(resource, definition.operation, scopeInputValidator, listFilterQueryValidator, operationInputs),
|
|
345
|
+
output: null,
|
|
346
|
+
extensions: Object.freeze({
|
|
347
|
+
assistant: createCrudAssistantExtension(resource, namespace, definition.operation)
|
|
348
|
+
}),
|
|
349
|
+
events: definition.events,
|
|
350
|
+
execute: definition.execute
|
|
351
|
+
});
|
|
352
|
+
});
|
|
333
353
|
const productActions = additionalActions.map((definition) => Object.freeze({
|
|
334
354
|
id: definition.id,
|
|
335
355
|
version: definition.version,
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { createSchema } from "json-rest-schema";
|
|
4
|
+
import { createServiceToolCatalog } from "@jskit-ai/assistant-core/server";
|
|
5
|
+
import { ActionProvider } from "@jskit-ai/kernel/server/actions";
|
|
6
|
+
import { EventProvider } from "@jskit-ai/kernel/server/runtime";
|
|
7
|
+
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
8
|
+
import { createCapabilityRuntime, defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
9
|
+
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
10
|
+
import { defineCrudJsonApiFeature } from "../src/server/defineCrudJsonApiFeature.js";
|
|
11
|
+
|
|
12
|
+
function createResource(namespace) {
|
|
13
|
+
return defineCrudResource({
|
|
14
|
+
namespace,
|
|
15
|
+
tableName: namespace,
|
|
16
|
+
crudOperations: ["list", "view"],
|
|
17
|
+
schema: {
|
|
18
|
+
reason: {
|
|
19
|
+
type: "string",
|
|
20
|
+
required: true,
|
|
21
|
+
operations: { output: { required: true } }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function valueProvider(id, capability, value) {
|
|
28
|
+
return defineProvider({
|
|
29
|
+
id,
|
|
30
|
+
provides: { value: capability },
|
|
31
|
+
setup: () => ({ value })
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Starts the CRUD features with stand-in database, HTTP and JSON:API capabilities, and returns runtime.actions. */
|
|
36
|
+
async function startFeatures(context, features) {
|
|
37
|
+
const jsonRestApi = {
|
|
38
|
+
resources: {},
|
|
39
|
+
async addResource(name, config) {
|
|
40
|
+
this.resources[name] = { config };
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
let actions;
|
|
44
|
+
const runtime = createCapabilityRuntime({
|
|
45
|
+
inputs: { "runtime.logger": silentLogger },
|
|
46
|
+
providers: [
|
|
47
|
+
EventProvider,
|
|
48
|
+
ActionProvider,
|
|
49
|
+
valueProvider("test.database", "runtime.database", { knex: Object.assign(() => {}, { transaction: (callback) => callback({}) }) }),
|
|
50
|
+
valueProvider("test.http", "runtime.http", { router: { register() {} } }),
|
|
51
|
+
valueProvider("test.json-rest-api", "runtime.json-rest-api", jsonRestApi),
|
|
52
|
+
...features,
|
|
53
|
+
defineProvider({
|
|
54
|
+
id: "test.observer",
|
|
55
|
+
requires: { catalogue: "runtime.actions" },
|
|
56
|
+
setup({ catalogue }) {
|
|
57
|
+
actions = catalogue;
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
]
|
|
62
|
+
});
|
|
63
|
+
await runtime.start();
|
|
64
|
+
context.after(() => runtime.shutdown());
|
|
65
|
+
return actions;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function toolActionIds(actions, permissions) {
|
|
69
|
+
const catalog = createServiceToolCatalog(actions, { maxDirectTools: 100 });
|
|
70
|
+
const toolSet = catalog.resolveToolSet({ actor: { id: "7" }, permissions, surface: "home" });
|
|
71
|
+
return toolSet.tools.map((tool) => tool.actionId).sort();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
test("the assistant reaches a CRUD action without HTTP routes only when a permission guards it", async (context) => {
|
|
75
|
+
const actions = await startFeatures(context, [
|
|
76
|
+
defineCrudJsonApiFeature({ resource: createResource("books"), surface: "home" }),
|
|
77
|
+
defineCrudJsonApiFeature({ resource: createResource("blocks"), surface: "home", internal: true, routes: false }),
|
|
78
|
+
defineCrudJsonApiFeature({ resource: createResource("drafts"), surface: "home", internal: true }),
|
|
79
|
+
defineCrudJsonApiFeature({
|
|
80
|
+
resource: createResource("audits"),
|
|
81
|
+
surface: "home",
|
|
82
|
+
routes: false,
|
|
83
|
+
permissions: { list: "app.admin" },
|
|
84
|
+
actions: {
|
|
85
|
+
summary: { kind: "query", input: { schema: createSchema({}) }, execute: async () => ({}) }
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
for (const id of ["crud.blocks.list", "crud.blocks.view", "crud.drafts.list", "crud.audits.view", "crud.audits.summary"]) {
|
|
91
|
+
assert.deepEqual(actions.getDefinition(id).channels, ["api", "internal"], id);
|
|
92
|
+
}
|
|
93
|
+
for (const id of ["crud.books.list", "crud.books.view", "crud.audits.list"]) {
|
|
94
|
+
assert.deepEqual(actions.getDefinition(id).channels, ["api", "automation", "internal"], id);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
assert.deepEqual(toolActionIds(actions, []), ["crud.books.list", "crud.books.view"]);
|
|
98
|
+
assert.deepEqual(toolActionIds(actions, ["app.admin"]), ["crud.audits.list", "crud.books.list", "crud.books.view"]);
|
|
99
|
+
});
|