adula 2.1.0 → 2.3.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 +149 -18
- package/build/agent/AGENTS.template.md +4 -4
- package/build/agent/capabilities.md +22 -12
- package/build/agent/skills/adula-frontend-design/SKILL.md +6 -4
- package/build/agent/skills/idea-review/SKILL.md +2 -2
- package/build/commands/adula_assistant_prune.d.ts +10 -0
- package/build/commands/adula_assistant_prune.js +12 -0
- package/build/commands/adula_outbox.d.ts +10 -0
- package/build/commands/adula_outbox.js +11 -0
- package/build/commands/adula_restore_reconcile.d.ts +10 -0
- package/build/commands/adula_restore_reconcile.js +34 -0
- package/build/commands/adula_runtime_health.d.ts +10 -0
- package/build/commands/adula_runtime_health.js +27 -0
- package/build/commands/adula_uploads_prune.d.ts +10 -0
- package/build/commands/adula_uploads_prune.js +16 -0
- package/build/commands/app.d.ts +10 -0
- package/build/commands/app.js +64 -0
- package/build/commands/backup_create.d.ts +11 -0
- package/build/commands/backup_create.js +99 -0
- package/build/commands/backup_open.d.ts +16 -0
- package/build/commands/backup_open.js +48 -0
- package/build/commands/backup_restore_files.d.ts +11 -0
- package/build/commands/backup_restore_files.js +44 -0
- package/build/commands/backup_restore_test.d.ts +12 -0
- package/build/commands/backup_restore_test.js +188 -0
- package/build/commands/backup_seal.d.ts +12 -0
- package/build/commands/backup_seal.js +36 -0
- package/build/commands/backup_services.d.ts +33 -0
- package/build/commands/backup_services.js +6 -0
- package/build/commands/backup_verify.d.ts +9 -0
- package/build/commands/backup_verify.js +59 -0
- package/build/commands/backup_verify_snapshot.d.ts +10 -0
- package/build/commands/backup_verify_snapshot.js +37 -0
- package/build/commands/client.d.ts +16 -0
- package/build/commands/client.js +71 -0
- package/build/commands/doctor.js +21 -1
- package/build/commands/install.d.ts +8 -0
- package/build/commands/install.js +69 -45
- package/build/commands/main.d.ts +3 -2
- package/build/commands/main.js +32 -0
- package/build/commands/module_add.js +4 -20
- package/build/commands/resource.d.ts +2 -0
- package/build/commands/resource.js +12 -3
- package/build/commands/resource_migration.d.ts +15 -0
- package/build/commands/resource_migration.js +81 -0
- package/build/commands/resource_snapshot.d.ts +2 -0
- package/build/commands/resource_snapshot.js +21 -4
- package/build/commands/sla_evaluate.d.ts +14 -0
- package/build/commands/sla_evaluate.js +16 -0
- package/build/commands/ui.js +4 -2
- package/build/commands/ui_css.d.ts +9 -0
- package/build/commands/ui_css.js +19 -0
- package/build/commands/upgrade.d.ts +1 -1
- package/build/commands/upgrade.js +1 -1
- package/build/configure.js +1 -1
- package/build/database/migrations/1770000000020_two_factor.d.ts +5 -0
- package/build/database/migrations/1770000000020_two_factor.js +10 -0
- package/build/database/migrations/1770000000041_import_modes.d.ts +5 -0
- package/build/database/migrations/1770000000041_import_modes.js +10 -0
- package/build/database/migrations/1770000000042_sync_jobs.d.ts +5 -0
- package/build/database/migrations/1770000000042_sync_jobs.js +10 -0
- package/build/database/migrations/1770000000043_notification_channels.d.ts +5 -0
- package/build/database/migrations/1770000000043_notification_channels.js +10 -0
- package/build/database/migrations/1770000000044_channel_confirmations.d.ts +5 -0
- package/build/database/migrations/1770000000044_channel_confirmations.js +10 -0
- package/build/database/migrations/1770000000045_import_leases.d.ts +5 -0
- package/build/database/migrations/1770000000045_import_leases.js +10 -0
- package/build/database/migrations/1770000000046_workflow_delegation_sla.d.ts +5 -0
- package/build/database/migrations/1770000000046_workflow_delegation_sla.js +10 -0
- package/build/database/migrations/1770000000049_print_templates.d.ts +5 -0
- package/build/database/migrations/1770000000049_print_templates.js +10 -0
- package/build/database/migrations/1770000000051_comment_notifications.d.ts +5 -0
- package/build/database/migrations/1770000000051_comment_notifications.js +10 -0
- package/build/database/migrations/1770000000054_cloud_backups.d.ts +5 -0
- package/build/database/migrations/1770000000054_cloud_backups.js +10 -0
- package/build/database/migrations/1770000000055_ai_assistant.d.ts +5 -0
- package/build/database/migrations/1770000000055_ai_assistant.js +10 -0
- package/build/database/migrations/1770000000056_appearance.d.ts +5 -0
- package/build/database/migrations/1770000000056_appearance.js +10 -0
- package/build/index.d.ts +87 -11
- package/build/index.js +50 -7
- package/build/providers/adula_provider.d.ts +2 -0
- package/build/providers/adula_provider.js +7 -0
- package/build/src/admin/contracts.js +1 -1
- package/build/src/admin/controller.d.ts +14 -1
- package/build/src/admin/controller.js +91 -13
- package/build/src/admin/errors.d.ts +32 -0
- package/build/src/admin/errors.js +63 -1
- package/build/src/admin/presentation.d.ts +21 -2
- package/build/src/admin/resource/editor.d.ts +60 -0
- package/build/src/admin/resource/editor.js +345 -0
- package/build/src/admin/resource/export.d.ts +54 -0
- package/build/src/admin/resource/export.js +262 -0
- package/build/src/admin/resource/lifecycle.d.ts +23 -0
- package/build/src/admin/resource/lifecycle.js +91 -0
- package/build/src/admin/resource/query.d.ts +111 -0
- package/build/src/admin/resource/query.js +595 -0
- package/build/src/admin/resource/read.d.ts +109 -0
- package/build/src/admin/resource/read.js +388 -0
- package/build/src/admin/resource/shared.d.ts +93 -0
- package/build/src/admin/resource/shared.js +265 -0
- package/build/src/admin/resource/trash.d.ts +10 -0
- package/build/src/admin/resource/trash.js +56 -0
- package/build/src/admin/resource/write.d.ts +57 -0
- package/build/src/admin/resource/write.js +603 -0
- package/build/src/admin/resource_service.d.ts +80 -193
- package/build/src/admin/resource_service.js +81 -1343
- package/build/src/admin/resource_tools.d.ts +111 -0
- package/build/src/admin/resource_tools.js +139 -0
- package/build/src/ai/assistant.d.ts +100 -0
- package/build/src/ai/assistant.js +487 -0
- package/build/src/ai/client.d.ts +49 -0
- package/build/src/ai/client.js +85 -0
- package/build/src/ai/prompt.d.ts +23 -0
- package/build/src/ai/prompt.js +83 -0
- package/build/src/ai/settings.d.ts +56 -0
- package/build/src/ai/settings.js +214 -0
- package/build/src/ai/usage.d.ts +16 -0
- package/build/src/ai/usage.js +33 -0
- package/build/src/appearance/appearance.d.ts +134 -0
- package/build/src/appearance/appearance.js +461 -0
- package/build/src/appearance/colors.d.ts +43 -0
- package/build/src/appearance/colors.js +99 -0
- package/build/src/appearance/logo.d.ts +32 -0
- package/build/src/appearance/logo.js +112 -0
- package/build/src/attachments/attachment_service.d.ts +46 -2
- package/build/src/attachments/attachment_service.js +96 -4
- package/build/src/auth/actor_store.d.ts +5 -1
- package/build/src/auth/actor_store.js +8 -4
- package/build/src/auth/conditions.d.ts +5 -0
- package/build/src/auth/conditions.js +26 -3
- package/build/src/auth/sql.js +17 -5
- package/build/src/auth/two_factor.d.ts +96 -0
- package/build/src/auth/two_factor.js +259 -0
- package/build/src/backups/archive.d.ts +52 -0
- package/build/src/backups/archive.js +362 -0
- package/build/src/backups/cloud_backups.d.ts +133 -0
- package/build/src/backups/cloud_backups.js +650 -0
- package/build/src/backups/doctor.d.ts +7 -0
- package/build/src/backups/doctor.js +55 -0
- package/build/src/backups/providers.d.ts +81 -0
- package/build/src/backups/providers.js +470 -0
- package/build/src/collaboration/assignments.d.ts +59 -2
- package/build/src/collaboration/assignments.js +196 -74
- package/build/src/collaboration/record_collaboration.d.ts +21 -4
- package/build/src/collaboration/record_collaboration.js +126 -51
- package/build/src/collaboration/shared.d.ts +24 -0
- package/build/src/collaboration/shared.js +34 -0
- package/build/src/commands/app_scaffold.d.ts +72 -0
- package/build/src/commands/app_scaffold.js +285 -0
- package/build/src/commands/capabilities.js +35 -9
- package/build/src/commands/doctor.d.ts +24 -2
- package/build/src/commands/doctor.js +81 -6
- package/build/src/commands/gap_report.d.ts +1 -1
- package/build/src/commands/gap_report.js +2 -2
- package/build/src/commands/generator.d.ts +37 -1
- package/build/src/commands/generator.js +61 -18
- package/build/src/commands/snapshot.d.ts +24 -13
- package/build/src/commands/snapshot.js +54 -14
- package/build/src/commands/source_markers.d.ts +10 -0
- package/build/src/commands/source_markers.js +29 -0
- package/build/src/core/activity.d.ts +2 -0
- package/build/src/core/activity.js +7 -1
- package/build/src/core/administrators.d.ts +38 -0
- package/build/src/core/administrators.js +145 -0
- package/build/src/core/avatar_image.d.ts +33 -0
- package/build/src/core/avatar_image.js +278 -0
- package/build/src/core/html_sanitizer.d.ts +20 -0
- package/build/src/core/html_sanitizer.js +261 -0
- package/build/src/core/like.d.ts +5 -0
- package/build/src/core/like.js +5 -0
- package/build/src/core/lookups.d.ts +80 -0
- package/build/src/core/lookups.js +319 -0
- package/build/src/core/mail_settings.d.ts +83 -0
- package/build/src/core/mail_settings.js +274 -0
- package/build/src/core/mail_test.d.ts +16 -0
- package/build/src/core/mail_test.js +81 -6
- package/build/src/core/message_templates.d.ts +15 -2
- package/build/src/core/message_templates.js +81 -11
- package/build/src/core/module_seed.js +2 -1
- package/build/src/core/notification_channels.d.ts +235 -0
- package/build/src/core/notification_channels.js +660 -0
- package/build/src/core/roles.js +26 -9
- package/build/src/core/saved_views.d.ts +8 -1
- package/build/src/core/saved_views.js +91 -26
- package/build/src/core/sequences.d.ts +32 -0
- package/build/src/core/sequences.js +97 -0
- package/build/src/core/settings.d.ts +5 -0
- package/build/src/core/settings.js +31 -3
- package/build/src/core/template_language.d.ts +61 -0
- package/build/src/core/template_language.js +141 -0
- package/build/src/core/ui_preferences.d.ts +10 -0
- package/build/src/core/ui_preferences.js +15 -1
- package/build/src/core/user_avatars.d.ts +79 -0
- package/build/src/core/user_avatars.js +186 -0
- package/build/src/core/user_invitations.d.ts +5 -1
- package/build/src/core/user_invitations.js +7 -1
- package/build/src/core/user_preferences.d.ts +36 -0
- package/build/src/core/user_preferences.js +125 -0
- package/build/src/core/users.d.ts +2 -0
- package/build/src/core/users.js +5 -3
- package/build/src/database/calendar_days.d.ts +19 -0
- package/build/src/database/calendar_days.js +36 -0
- package/build/src/database/schema.d.ts +201 -2
- package/build/src/database/schema.js +713 -59
- package/build/src/database/schema_diff.d.ts +70 -0
- package/build/src/database/schema_diff.js +190 -0
- package/build/src/events/record_mutation.d.ts +2 -1
- package/build/src/events/record_mutation.js +1 -0
- package/build/src/integrations/api_client.d.ts +37 -0
- package/build/src/integrations/api_client.js +567 -0
- package/build/src/integrations/import_adapters.d.ts +37 -0
- package/build/src/integrations/import_adapters.js +35 -0
- package/build/src/integrations/imports.d.ts +130 -16
- package/build/src/integrations/imports.js +434 -73
- package/build/src/integrations/openapi.d.ts +3 -1
- package/build/src/integrations/openapi.js +465 -31
- package/build/src/integrations/outbound.d.ts +94 -0
- package/build/src/integrations/outbound.js +276 -0
- package/build/src/integrations/print.d.ts +22 -1
- package/build/src/integrations/print.js +68 -22
- package/build/src/integrations/print_templates.d.ts +216 -0
- package/build/src/integrations/print_templates.js +1017 -0
- package/build/src/integrations/sync_jobs.d.ts +139 -0
- package/build/src/integrations/sync_jobs.js +416 -0
- package/build/src/integrations/webhooks.d.ts +12 -12
- package/build/src/integrations/webhooks.js +34 -43
- package/build/src/integrations/xlsx.d.ts +41 -0
- package/build/src/integrations/xlsx.js +519 -0
- package/build/src/languages/ar.d.ts +11 -0
- package/build/src/languages/ar.js +358 -0
- package/build/src/languages/ar_ui.d.ts +461 -0
- package/build/src/languages/ar_ui.js +464 -0
- package/build/src/languages/catalog.d.ts +66 -0
- package/build/src/languages/catalog.js +152 -0
- package/build/src/languages/en.d.ts +7 -0
- package/build/src/languages/en.js +352 -0
- package/build/src/languages/en_ui.d.ts +3 -0
- package/build/src/languages/en_ui.js +458 -0
- package/build/src/languages/validation.d.ts +26 -0
- package/build/src/languages/validation.js +60 -0
- package/build/src/lists/bulk_actions.d.ts +89 -0
- package/build/src/lists/bulk_actions.js +268 -0
- package/build/src/lists/dashboards.d.ts +138 -0
- package/build/src/lists/dashboards.js +290 -0
- package/build/src/mcp/tools.js +9 -104
- package/build/src/resource/define_resource.d.ts +18 -1
- package/build/src/resource/define_resource.js +212 -18
- package/build/src/resource/definition.d.ts +44 -0
- package/build/src/resource/definition.js +171 -0
- package/build/src/resource/registry.js +15 -4
- package/build/src/resource/types.d.ts +313 -16
- package/build/src/resource/values.d.ts +56 -1
- package/build/src/resource/values.js +237 -5
- package/build/src/services/settings.d.ts +2 -0
- package/build/src/services/settings.js +5 -1
- package/build/src/upgrade/legacy.js +2 -2
- package/build/src/workflows/calendar.d.ts +9 -0
- package/build/src/workflows/calendar.js +44 -0
- package/build/src/workflows/define_workflow.d.ts +48 -3
- package/build/src/workflows/define_workflow.js +57 -13
- package/build/src/workflows/delegations.d.ts +107 -0
- package/build/src/workflows/delegations.js +326 -0
- package/build/src/workflows/engine.d.ts +80 -4
- package/build/src/workflows/engine.js +466 -86
- package/build/src/workflows/sla.d.ts +15 -0
- package/build/src/workflows/sla.js +8 -0
- package/build/stubs/resource_contract.txt +12 -1
- package/package.json +9 -2
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { buildAbility } from '../auth/ability.js';
|
|
2
|
+
import { canQueryField } from '../admin/resource/shared.js';
|
|
3
|
+
import { AdulaError, notFound } from '../admin/errors.js';
|
|
4
|
+
import { labelText, translate } from '../languages/catalog.js';
|
|
5
|
+
import { cellText } from '../admin/resource/export.js';
|
|
6
|
+
/** Field types a widget may total, as ResourceService.aggregate sums them. */
|
|
7
|
+
const SUMMABLE = ['integer', 'money', 'decimal', 'percent'];
|
|
8
|
+
const NAME = /^[a-z][a-z0-9_]*$/;
|
|
9
|
+
const KEY = /^[a-z][a-zA-Z0-9_]*$/;
|
|
10
|
+
/** Declares an indicators page outside the resource type (ADR 046). */
|
|
11
|
+
export function defineDashboard(definition) {
|
|
12
|
+
const { name } = definition ?? {};
|
|
13
|
+
if (typeof name !== 'string' || !NAME.test(name))
|
|
14
|
+
throw new Error(`Invalid dashboard name: ${String(name)}`);
|
|
15
|
+
if (!definition.label?.ar?.trim())
|
|
16
|
+
throw new Error(`${name}: label.ar is required`);
|
|
17
|
+
if (definition.description !== undefined && !definition.description?.ar?.trim())
|
|
18
|
+
throw new Error(`${name}: description.ar is required`);
|
|
19
|
+
if (!Array.isArray(definition.widgets) || !definition.widgets.length)
|
|
20
|
+
throw new Error(`${name}: a dashboard needs at least one widget`);
|
|
21
|
+
const keys = new Set();
|
|
22
|
+
for (const widget of definition.widgets) {
|
|
23
|
+
if (typeof widget?.key !== 'string' || !KEY.test(widget.key))
|
|
24
|
+
throw new Error(`${name}: invalid widget key ${String(widget?.key)}`);
|
|
25
|
+
if (keys.has(widget.key))
|
|
26
|
+
throw new Error(`${name}: duplicate widget ${widget.key}`);
|
|
27
|
+
keys.add(widget.key);
|
|
28
|
+
const where = `${name}.${widget.key}`;
|
|
29
|
+
if (!widget.label?.ar?.trim())
|
|
30
|
+
throw new Error(`${where}: label.ar is required`);
|
|
31
|
+
if (typeof widget.resource !== 'string' || !widget.resource)
|
|
32
|
+
throw new Error(`${where}: resource is required`);
|
|
33
|
+
const measure = widget.measure;
|
|
34
|
+
if (measure !== 'count' &&
|
|
35
|
+
!(measure &&
|
|
36
|
+
typeof measure === 'object' &&
|
|
37
|
+
Object.keys(measure).length === 1 &&
|
|
38
|
+
typeof measure.sum === 'string'))
|
|
39
|
+
throw new Error(`${where}: measure is 'count' or { sum: field }`);
|
|
40
|
+
if (widget.groupBy !== undefined && typeof widget.groupBy !== 'string')
|
|
41
|
+
throw new Error(`${where}: groupBy is one field name`);
|
|
42
|
+
if (widget.overview !== undefined && typeof widget.overview !== 'boolean')
|
|
43
|
+
throw new Error(`${where}: overview is true or false`);
|
|
44
|
+
for (const [option, value] of [
|
|
45
|
+
['filters', widget.filters],
|
|
46
|
+
['where', widget.where],
|
|
47
|
+
])
|
|
48
|
+
if (value !== undefined && (!value || typeof value !== 'object' || Array.isArray(value)))
|
|
49
|
+
throw new Error(`${where}: ${option} must be an object`);
|
|
50
|
+
}
|
|
51
|
+
return Object.freeze({ ...definition, widgets: Object.freeze([...definition.widgets]) });
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Renders dashboards as the viewer: every widget runs through ResourceService.aggregate
|
|
55
|
+
* with the viewer's rules, scope and field access, and a widget the viewer cannot read
|
|
56
|
+
* is omitted rather than failed.
|
|
57
|
+
*/
|
|
58
|
+
export class Dashboards {
|
|
59
|
+
db;
|
|
60
|
+
registry;
|
|
61
|
+
resources;
|
|
62
|
+
dashboards;
|
|
63
|
+
constructor(db, registry, resources, definitions = []) {
|
|
64
|
+
this.db = db;
|
|
65
|
+
this.registry = registry;
|
|
66
|
+
this.resources = resources;
|
|
67
|
+
this.dashboards = Dashboards.validate(registry, definitions);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Checks dashboards against the registered resources: unique names, existing widget
|
|
71
|
+
* resources, integer, money, decimal or percent totals, groupable fields and filter or condition keys
|
|
72
|
+
* that exist on the resource. Call it when the application loads its definitions, so a
|
|
73
|
+
* bad definition fails at boot.
|
|
74
|
+
*/
|
|
75
|
+
static validate(registry, definitions) {
|
|
76
|
+
const dashboards = new Map();
|
|
77
|
+
for (const dashboard of definitions) {
|
|
78
|
+
if (dashboards.has(dashboard.name))
|
|
79
|
+
throw new Error(`Duplicate dashboard: ${dashboard.name}`);
|
|
80
|
+
for (const widget of dashboard.widgets) {
|
|
81
|
+
const resource = registry.get(widget.resource);
|
|
82
|
+
const where = `${dashboard.name}.${widget.key}`;
|
|
83
|
+
if (typeof widget.measure === 'object') {
|
|
84
|
+
const field = resource.fields[widget.measure.sum];
|
|
85
|
+
if (!field || !SUMMABLE.includes(field.type))
|
|
86
|
+
throw new Error(`${where}: ${widget.measure.sum} is not an integer, money, decimal or percent field`);
|
|
87
|
+
}
|
|
88
|
+
if (widget.groupBy !== undefined && !groupable(resource, widget.groupBy))
|
|
89
|
+
throw new Error(`${where}: ${widget.groupBy} cannot be grouped`);
|
|
90
|
+
for (const key of Object.keys(widget.filters ?? {}))
|
|
91
|
+
if (!Object.hasOwn(resource.fields, key) || !resource.fields[key].filterable)
|
|
92
|
+
throw new Error(`${where}: ${key} is not a filterable field`);
|
|
93
|
+
for (const key of Object.keys(widget.where ?? {}))
|
|
94
|
+
if (!groupable(resource, key))
|
|
95
|
+
throw new Error(`${where}: ${key} cannot be a condition`);
|
|
96
|
+
}
|
|
97
|
+
dashboards.set(dashboard.name, dashboard);
|
|
98
|
+
}
|
|
99
|
+
return dashboards;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Whether the actor may read this widget: the resource is viewable and every field it
|
|
103
|
+
* totals, groups, filters or conditions on is one the actor may query.
|
|
104
|
+
*/
|
|
105
|
+
readable(widget, actor, ability) {
|
|
106
|
+
const resource = this.registry.get(widget.resource);
|
|
107
|
+
const standard = new Set(['docStatus', 'orgUnitId']);
|
|
108
|
+
const fields = [
|
|
109
|
+
...(typeof widget.measure === 'object' ? [widget.measure.sum] : []),
|
|
110
|
+
...(widget.groupBy ? [widget.groupBy] : []),
|
|
111
|
+
...Object.keys(widget.filters ?? {}),
|
|
112
|
+
...Object.keys(widget.where ?? {}),
|
|
113
|
+
].filter((key) => !standard.has(key));
|
|
114
|
+
return (resource.actions.includes('view') &&
|
|
115
|
+
ability.can('view', resource.name) &&
|
|
116
|
+
(!resource.scoped || actor.orgPaths.length > 0) &&
|
|
117
|
+
fields.every((key) => canQueryField(resource, actor, ability, key)));
|
|
118
|
+
}
|
|
119
|
+
/** Dashboards with at least one widget the actor may read. */
|
|
120
|
+
list(actor) {
|
|
121
|
+
const ability = buildAbility(actor.rules, this.registry.all());
|
|
122
|
+
return [...this.dashboards.values()]
|
|
123
|
+
.filter((dashboard) => dashboard.widgets.some((widget) => this.readable(widget, actor, ability)))
|
|
124
|
+
.map((dashboard) => ({ name: dashboard.name, label: labelText(dashboard.label) }));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The personal overview (ADR 050): the host's built-in widgets and every `overview: true`
|
|
128
|
+
* dashboard widget the actor may read, arranged by the saved layout. Saved ids that are
|
|
129
|
+
* unknown or no longer readable are dropped, widgets added later join in default order, and
|
|
130
|
+
* only shown dashboard widgets are computed, so a stored id never reveals data.
|
|
131
|
+
*/
|
|
132
|
+
async overview(actor, saved, builtins = []) {
|
|
133
|
+
const ability = buildAbility(actor.rules, this.registry.all());
|
|
134
|
+
const available = new Map();
|
|
135
|
+
for (const builtin of builtins)
|
|
136
|
+
available.set(builtin.id, { label: builtin.label, builtin: true });
|
|
137
|
+
for (const dashboard of this.dashboards.values())
|
|
138
|
+
for (const widget of dashboard.widgets)
|
|
139
|
+
if (widget.overview && this.readable(widget, actor, ability))
|
|
140
|
+
available.set(`dashboard.${dashboard.name}.${widget.key}`, {
|
|
141
|
+
label: labelText(widget.label),
|
|
142
|
+
dashboard: dashboard.name,
|
|
143
|
+
widget,
|
|
144
|
+
});
|
|
145
|
+
const ids = (value) => Array.isArray(value)
|
|
146
|
+
? value.filter((id) => typeof id === 'string' && available.has(id))
|
|
147
|
+
: [];
|
|
148
|
+
const order = [...new Set([...ids(saved?.order), ...available.keys()])];
|
|
149
|
+
const hidden = new Set(ids(saved?.hidden));
|
|
150
|
+
const defaults = [...available.keys()];
|
|
151
|
+
const widgets = [];
|
|
152
|
+
for (const id of order) {
|
|
153
|
+
const entry = available.get(id);
|
|
154
|
+
const isHidden = hidden.has(id);
|
|
155
|
+
if (entry.builtin) {
|
|
156
|
+
widgets.push({ id, kind: 'builtin', label: entry.label, hidden: isHidden });
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
widgets.push({
|
|
160
|
+
id,
|
|
161
|
+
kind: 'dashboard',
|
|
162
|
+
label: entry.label,
|
|
163
|
+
hidden: isHidden,
|
|
164
|
+
dashboard: entry.dashboard,
|
|
165
|
+
data: isHidden ? null : await this.widget(entry.widget, actor),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
widgets,
|
|
170
|
+
customized: hidden.size > 0 || order.some((id, index) => id !== defaults[index]),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
async render(name, actor) {
|
|
174
|
+
const dashboard = typeof name === 'string' ? this.dashboards.get(name) : undefined;
|
|
175
|
+
if (!dashboard)
|
|
176
|
+
throw notFound('لوحة المؤشرات غير موجودة');
|
|
177
|
+
const widgets = [];
|
|
178
|
+
for (const widget of dashboard.widgets) {
|
|
179
|
+
const rendered = await this.widget(widget, actor);
|
|
180
|
+
if (rendered)
|
|
181
|
+
widgets.push(rendered);
|
|
182
|
+
}
|
|
183
|
+
// A dashboard whose widgets are all hidden from the viewer does not exist for them.
|
|
184
|
+
if (!widgets.length)
|
|
185
|
+
throw notFound('لوحة المؤشرات غير موجودة');
|
|
186
|
+
return {
|
|
187
|
+
name: dashboard.name,
|
|
188
|
+
label: labelText(dashboard.label),
|
|
189
|
+
description: dashboard.description ? labelText(dashboard.description) : null,
|
|
190
|
+
widgets,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
async widget(widget, actor) {
|
|
194
|
+
const resource = this.registry.get(widget.resource);
|
|
195
|
+
// A widget filtered or conditioned on a field the viewer may not query is omitted,
|
|
196
|
+
// never a refusal of the whole page.
|
|
197
|
+
if (!this.readable(widget, actor, buildAbility(actor.rules, this.registry.all())))
|
|
198
|
+
return null;
|
|
199
|
+
const sum = typeof widget.measure === 'object' ? widget.measure.sum : null;
|
|
200
|
+
let aggregate;
|
|
201
|
+
try {
|
|
202
|
+
aggregate = await this.resources.aggregate(resource.name, actor, {
|
|
203
|
+
groupBy: widget.groupBy ? [widget.groupBy] : [],
|
|
204
|
+
count: sum === null,
|
|
205
|
+
sum: sum ? [sum] : [],
|
|
206
|
+
filters: widget.filters,
|
|
207
|
+
where: widget.where,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (error instanceof AdulaError && error.status === 403)
|
|
212
|
+
return null;
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
const measureOf = (row) => sum === null ? (row.count ?? 0) : (row.sum?.[sum] ?? null);
|
|
216
|
+
const base = {
|
|
217
|
+
key: widget.key,
|
|
218
|
+
label: labelText(widget.label),
|
|
219
|
+
resource: resource.name,
|
|
220
|
+
href: `/resources/${resource.name}`,
|
|
221
|
+
measure: widget.measure,
|
|
222
|
+
format: (sum === null ? 'count' : resource.fields[sum].type),
|
|
223
|
+
groupBy: widget.groupBy ?? null,
|
|
224
|
+
truncated: aggregate.truncated,
|
|
225
|
+
};
|
|
226
|
+
if (!widget.groupBy) {
|
|
227
|
+
const [row] = aggregate.rows;
|
|
228
|
+
return { ...base, kind: 'total', value: row ? measureOf(row) : sum === null ? 0 : null };
|
|
229
|
+
}
|
|
230
|
+
const key = widget.groupBy;
|
|
231
|
+
const values = aggregate.rows.map((row) => row.group[key] ?? null);
|
|
232
|
+
const label = await this.labeller(resource, key, values, actor);
|
|
233
|
+
return {
|
|
234
|
+
...base,
|
|
235
|
+
kind: 'grouped',
|
|
236
|
+
rows: aggregate.rows.map((row, index) => ({
|
|
237
|
+
value: values[index],
|
|
238
|
+
label: label(values[index]),
|
|
239
|
+
measure: measureOf(row),
|
|
240
|
+
})),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/** Display labels of one grouped field: lookups, titles, user names, «نعم/لا». */
|
|
244
|
+
async labeller(resource, key, values, actor) {
|
|
245
|
+
const none = translate('list.value.none');
|
|
246
|
+
const ids = [
|
|
247
|
+
...new Set(values.filter((value) => typeof value === 'number')),
|
|
248
|
+
].filter((id) => Number.isSafeInteger(id) && id > 0);
|
|
249
|
+
if (key === 'docStatus')
|
|
250
|
+
return (value) => value === null ? none : translate(`list.status.${Number(value)}`);
|
|
251
|
+
if (key === 'orgUnitId') {
|
|
252
|
+
const names = new Map();
|
|
253
|
+
if (ids.length)
|
|
254
|
+
for (const row of await this.db('org_units').whereIn('id', ids).select('id', 'name'))
|
|
255
|
+
names.set(Number(row.id), String(row.name));
|
|
256
|
+
return (value) => value === null ? none : (names.get(Number(value)) ?? `#${value}`);
|
|
257
|
+
}
|
|
258
|
+
const field = resource.fields[key];
|
|
259
|
+
const titles = new Map();
|
|
260
|
+
const lookups = new Map();
|
|
261
|
+
if (field.type === 'belongsTo' && ids.length)
|
|
262
|
+
for (const [id, title] of await this.resources.titles(field.resource, ids, actor))
|
|
263
|
+
titles.set(id, title);
|
|
264
|
+
if (field.type === 'user' && ids.length)
|
|
265
|
+
// A display name only, as list() shows it: readers need not read account e-mails.
|
|
266
|
+
for (const row of await this.db('users').whereIn('id', ids).select('id', 'full_name'))
|
|
267
|
+
if (row.full_name)
|
|
268
|
+
titles.set(Number(row.id), String(row.full_name));
|
|
269
|
+
if (field.type === 'lookup')
|
|
270
|
+
for (const row of await this.db('lookups')
|
|
271
|
+
.where('group', field.group)
|
|
272
|
+
.select('key', 'label_ar'))
|
|
273
|
+
lookups.set(String(row.key), String(row.label_ar));
|
|
274
|
+
return (value) => value === null || value === ''
|
|
275
|
+
? none
|
|
276
|
+
: cellText(field, value, {
|
|
277
|
+
lookup: (_, entry) => lookups.get(entry),
|
|
278
|
+
title: (id) => titles.get(id),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function groupable(resource, key) {
|
|
283
|
+
if (key === 'docStatus')
|
|
284
|
+
return Boolean(resource.submittable);
|
|
285
|
+
if (key === 'orgUnitId')
|
|
286
|
+
return resource.scoped;
|
|
287
|
+
const field = Object.hasOwn(resource.fields, key) ? resource.fields[key] : undefined;
|
|
288
|
+
// As aggregate(): a manyToMany field has no single value to group or compare.
|
|
289
|
+
return Boolean(field) && !['hasMany', 'manyToMany', 'json', 'attachment'].includes(field.type);
|
|
290
|
+
}
|
package/build/src/mcp/tools.js
CHANGED
|
@@ -1,88 +1,26 @@
|
|
|
1
1
|
import { Tool } from '@jrmc/adonis-mcp';
|
|
2
2
|
import { AdulaError } from '../admin/errors.js';
|
|
3
|
-
|
|
4
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
5
|
-
throw new AdulaError(422, 'E_INPUT', 'Expected an object');
|
|
6
|
-
return value;
|
|
7
|
-
}
|
|
8
|
-
function recordId(value) {
|
|
9
|
-
if (!Number.isSafeInteger(value) || Number(value) < 1)
|
|
10
|
-
throw new AdulaError(422, 'E_ID', 'Expected a positive integer record ID');
|
|
11
|
-
return Number(value);
|
|
12
|
-
}
|
|
3
|
+
import { RESOURCE_READ_SCHEMA, RESOURCE_WRITE_SCHEMA, parseResourceToolArgs, resourceToolError, runResourceTool, } from '../admin/resource_tools.js';
|
|
13
4
|
/** Optional MCP adapter. Actor identity comes only from the host's authenticated context. */
|
|
14
5
|
export function createResourceTools(resolveRuntime) {
|
|
15
6
|
async function execute(context, write) {
|
|
16
7
|
try {
|
|
17
|
-
|
|
8
|
+
// Argument shape is refused before the runtime (and the actor) is resolved.
|
|
9
|
+
const args = context.args;
|
|
10
|
+
if (!args || typeof args !== 'object' || Array.isArray(args))
|
|
11
|
+
throw new AdulaError(422, 'E_INPUT', 'Expected an object');
|
|
18
12
|
const accepted = write
|
|
19
13
|
? ['resource', 'action', 'id', 'input', 'version']
|
|
20
14
|
: ['resource', 'action', 'id', 'options'];
|
|
21
15
|
if (Object.keys(args).some((key) => !accepted.includes(key)))
|
|
22
16
|
throw new AdulaError(422, 'E_INPUT', 'Unknown tool argument');
|
|
23
17
|
const runtime = await resolveRuntime(context);
|
|
24
|
-
|
|
25
|
-
!runtime.registry.all().some((r) => r.name === args.resource))
|
|
26
|
-
throw new AdulaError(404, 'E_NOT_FOUND', 'الكيان غير موجود');
|
|
27
|
-
const name = args.resource;
|
|
28
|
-
const { resources, actor } = runtime;
|
|
29
|
-
let result;
|
|
30
|
-
if (!write && args.action === 'list') {
|
|
31
|
-
const options = args.options === undefined ? {} : object(args.options);
|
|
32
|
-
if (Object.keys(options).some((key) => !['limit', 'cursor', 'search', 'sort', 'direction', 'filters', 'estimate'].includes(key)))
|
|
33
|
-
throw new AdulaError(422, 'E_INPUT', 'Unknown list option');
|
|
34
|
-
for (const key of ['cursor', 'search', 'sort'])
|
|
35
|
-
if (options[key] !== undefined && typeof options[key] !== 'string')
|
|
36
|
-
throw new AdulaError(422, 'E_INPUT', `Invalid ${key}`);
|
|
37
|
-
if ((options.limit !== undefined && !Number.isSafeInteger(options.limit)) ||
|
|
38
|
-
(options.direction !== undefined &&
|
|
39
|
-
!['asc', 'desc'].includes(String(options.direction))) ||
|
|
40
|
-
(options.estimate !== undefined && typeof options.estimate !== 'boolean'))
|
|
41
|
-
throw new AdulaError(422, 'E_INPUT', 'Invalid list options');
|
|
42
|
-
if (options.filters !== undefined)
|
|
43
|
-
object(options.filters);
|
|
44
|
-
result = await resources.list(name, actor, options);
|
|
45
|
-
}
|
|
46
|
-
else if (!write && args.action === 'show') {
|
|
47
|
-
result = await resources.show(name, recordId(args.id), actor);
|
|
48
|
-
}
|
|
49
|
-
else if (!write && args.action === 'editor') {
|
|
50
|
-
result = await resources.editor(name, actor, args.id === undefined ? undefined : recordId(args.id));
|
|
51
|
-
}
|
|
52
|
-
else if (write && (args.action === 'create' || args.action === 'update')) {
|
|
53
|
-
if (args.action === 'create' && args.id !== undefined)
|
|
54
|
-
throw new AdulaError(422, 'E_INPUT', 'Create cannot specify an ID');
|
|
55
|
-
result = {
|
|
56
|
-
data: await resources.save(name, actor, object(args.input), args.action === 'update' ? recordId(args.id) : undefined),
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
else if (write &&
|
|
60
|
-
(args.action === 'delete' || args.action === 'submit' || args.action === 'cancel')) {
|
|
61
|
-
result = {
|
|
62
|
-
data: await resources.transition(name, recordId(args.id), actor, args.action, args.version),
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
else
|
|
66
|
-
throw new AdulaError(422, 'E_ACTION', 'Unsupported resource action');
|
|
18
|
+
const result = await runResourceTool(runtime, parseResourceToolArgs(runtime, args, write));
|
|
67
19
|
return context.response.text(JSON.stringify(result));
|
|
68
20
|
}
|
|
69
21
|
catch (error) {
|
|
70
|
-
if (error instanceof AdulaError)
|
|
71
|
-
return context.response.error(JSON.stringify({ status: error.status, code: error.code, message: error.message }));
|
|
72
|
-
if (error?.code === 'E_VALIDATION_ERROR')
|
|
73
|
-
return context.response.error(JSON.stringify({
|
|
74
|
-
status: 422,
|
|
75
|
-
code: 'E_VALIDATION_ERROR',
|
|
76
|
-
messages: error.messages,
|
|
77
|
-
}));
|
|
78
|
-
if (['23505', '23503'].includes(error?.code ?? ''))
|
|
79
|
-
return context.response.error(JSON.stringify({
|
|
80
|
-
status: 422,
|
|
81
|
-
code: 'E_CONSTRAINT',
|
|
82
|
-
message: 'تحقق من القيم والسجلات المرتبطة',
|
|
83
|
-
}));
|
|
84
22
|
// Do not put database statements, credentials or stack traces on the wire.
|
|
85
|
-
return context.response.error(JSON.stringify(
|
|
23
|
+
return context.response.error(JSON.stringify(resourceToolError(error)));
|
|
86
24
|
}
|
|
87
25
|
}
|
|
88
26
|
class ResourceReadTool extends Tool {
|
|
@@ -96,29 +34,7 @@ export function createResourceTools(resolveRuntime) {
|
|
|
96
34
|
openWorldHint: false,
|
|
97
35
|
};
|
|
98
36
|
schema() {
|
|
99
|
-
return
|
|
100
|
-
type: 'object',
|
|
101
|
-
additionalProperties: false,
|
|
102
|
-
properties: {
|
|
103
|
-
resource: { type: 'string' },
|
|
104
|
-
action: { type: 'string', enum: ['list', 'show', 'editor'] },
|
|
105
|
-
id: { type: 'integer', minimum: 1 },
|
|
106
|
-
options: {
|
|
107
|
-
type: 'object',
|
|
108
|
-
properties: {
|
|
109
|
-
limit: { type: 'integer', minimum: 1, maximum: 100 },
|
|
110
|
-
cursor: { type: 'string' },
|
|
111
|
-
search: { type: 'string' },
|
|
112
|
-
sort: { type: 'string' },
|
|
113
|
-
direction: { type: 'string', enum: ['asc', 'desc'] },
|
|
114
|
-
filters: { type: 'object', properties: {}, additionalProperties: true },
|
|
115
|
-
estimate: { type: 'boolean' },
|
|
116
|
-
},
|
|
117
|
-
additionalProperties: false,
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
required: ['resource', 'action'],
|
|
121
|
-
};
|
|
37
|
+
return structuredClone(RESOURCE_READ_SCHEMA);
|
|
122
38
|
}
|
|
123
39
|
async handle(context) {
|
|
124
40
|
return execute(context, false);
|
|
@@ -135,18 +51,7 @@ export function createResourceTools(resolveRuntime) {
|
|
|
135
51
|
openWorldHint: false,
|
|
136
52
|
};
|
|
137
53
|
schema() {
|
|
138
|
-
return
|
|
139
|
-
type: 'object',
|
|
140
|
-
additionalProperties: false,
|
|
141
|
-
properties: {
|
|
142
|
-
resource: { type: 'string' },
|
|
143
|
-
action: { type: 'string', enum: ['create', 'update', 'delete', 'submit', 'cancel'] },
|
|
144
|
-
id: { type: 'integer', minimum: 1 },
|
|
145
|
-
input: { type: 'object', properties: {}, additionalProperties: true },
|
|
146
|
-
version: { type: 'integer', minimum: 1 },
|
|
147
|
-
},
|
|
148
|
-
required: ['resource', 'action'],
|
|
149
|
-
};
|
|
54
|
+
return structuredClone(RESOURCE_WRITE_SCHEMA);
|
|
150
55
|
}
|
|
151
56
|
async handle(context) {
|
|
152
57
|
return execute(context, true);
|
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
import type { Field, Resource, ResourceInput } from './types.js';
|
|
1
|
+
import type { Field, Label, Resource, ResourceInput } from './types.js';
|
|
2
2
|
export declare function identifier(value: string): string;
|
|
3
3
|
/** camelCase field key → snake_case column; validated once and memoized (hot in row mapping). */
|
|
4
4
|
export declare function columnName(name: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* The join table of a manyToMany field: `table`, or `<owner>_<key in snake_case>`. Its
|
|
7
|
+
* columns are `<owner>_id` and `<target>_id` (`related_id` when a resource links itself).
|
|
8
|
+
*/
|
|
9
|
+
export declare function joinTable(owner: string, key: string, field: {
|
|
10
|
+
resource: string;
|
|
11
|
+
table?: string;
|
|
12
|
+
}): {
|
|
13
|
+
table: string;
|
|
14
|
+
owner: string;
|
|
15
|
+
target: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Arabic is required; every other language is optional, but a language a label
|
|
19
|
+
* names must have text (an empty `en: ''` is a mistake, not an omission).
|
|
20
|
+
*/
|
|
21
|
+
export declare function assertLabel(label: Label | undefined, context: string): void;
|
|
5
22
|
export declare function defineResource<const F extends Record<string, Field>>(input: ResourceInput<F>): ResourceInput<F> & Resource;
|