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.
Files changed (269) hide show
  1. package/README.md +149 -18
  2. package/build/agent/AGENTS.template.md +4 -4
  3. package/build/agent/capabilities.md +22 -12
  4. package/build/agent/skills/adula-frontend-design/SKILL.md +6 -4
  5. package/build/agent/skills/idea-review/SKILL.md +2 -2
  6. package/build/commands/adula_assistant_prune.d.ts +10 -0
  7. package/build/commands/adula_assistant_prune.js +12 -0
  8. package/build/commands/adula_outbox.d.ts +10 -0
  9. package/build/commands/adula_outbox.js +11 -0
  10. package/build/commands/adula_restore_reconcile.d.ts +10 -0
  11. package/build/commands/adula_restore_reconcile.js +34 -0
  12. package/build/commands/adula_runtime_health.d.ts +10 -0
  13. package/build/commands/adula_runtime_health.js +27 -0
  14. package/build/commands/adula_uploads_prune.d.ts +10 -0
  15. package/build/commands/adula_uploads_prune.js +16 -0
  16. package/build/commands/app.d.ts +10 -0
  17. package/build/commands/app.js +64 -0
  18. package/build/commands/backup_create.d.ts +11 -0
  19. package/build/commands/backup_create.js +99 -0
  20. package/build/commands/backup_open.d.ts +16 -0
  21. package/build/commands/backup_open.js +48 -0
  22. package/build/commands/backup_restore_files.d.ts +11 -0
  23. package/build/commands/backup_restore_files.js +44 -0
  24. package/build/commands/backup_restore_test.d.ts +12 -0
  25. package/build/commands/backup_restore_test.js +188 -0
  26. package/build/commands/backup_seal.d.ts +12 -0
  27. package/build/commands/backup_seal.js +36 -0
  28. package/build/commands/backup_services.d.ts +33 -0
  29. package/build/commands/backup_services.js +6 -0
  30. package/build/commands/backup_verify.d.ts +9 -0
  31. package/build/commands/backup_verify.js +59 -0
  32. package/build/commands/backup_verify_snapshot.d.ts +10 -0
  33. package/build/commands/backup_verify_snapshot.js +37 -0
  34. package/build/commands/client.d.ts +16 -0
  35. package/build/commands/client.js +71 -0
  36. package/build/commands/doctor.js +21 -1
  37. package/build/commands/install.d.ts +8 -0
  38. package/build/commands/install.js +69 -45
  39. package/build/commands/main.d.ts +3 -2
  40. package/build/commands/main.js +32 -0
  41. package/build/commands/module_add.js +4 -20
  42. package/build/commands/resource.d.ts +2 -0
  43. package/build/commands/resource.js +12 -3
  44. package/build/commands/resource_migration.d.ts +15 -0
  45. package/build/commands/resource_migration.js +81 -0
  46. package/build/commands/resource_snapshot.d.ts +2 -0
  47. package/build/commands/resource_snapshot.js +21 -4
  48. package/build/commands/sla_evaluate.d.ts +14 -0
  49. package/build/commands/sla_evaluate.js +16 -0
  50. package/build/commands/ui.js +4 -2
  51. package/build/commands/ui_css.d.ts +9 -0
  52. package/build/commands/ui_css.js +19 -0
  53. package/build/commands/upgrade.d.ts +1 -1
  54. package/build/commands/upgrade.js +1 -1
  55. package/build/configure.js +1 -1
  56. package/build/database/migrations/1770000000020_two_factor.d.ts +5 -0
  57. package/build/database/migrations/1770000000020_two_factor.js +10 -0
  58. package/build/database/migrations/1770000000041_import_modes.d.ts +5 -0
  59. package/build/database/migrations/1770000000041_import_modes.js +10 -0
  60. package/build/database/migrations/1770000000042_sync_jobs.d.ts +5 -0
  61. package/build/database/migrations/1770000000042_sync_jobs.js +10 -0
  62. package/build/database/migrations/1770000000043_notification_channels.d.ts +5 -0
  63. package/build/database/migrations/1770000000043_notification_channels.js +10 -0
  64. package/build/database/migrations/1770000000044_channel_confirmations.d.ts +5 -0
  65. package/build/database/migrations/1770000000044_channel_confirmations.js +10 -0
  66. package/build/database/migrations/1770000000045_import_leases.d.ts +5 -0
  67. package/build/database/migrations/1770000000045_import_leases.js +10 -0
  68. package/build/database/migrations/1770000000046_workflow_delegation_sla.d.ts +5 -0
  69. package/build/database/migrations/1770000000046_workflow_delegation_sla.js +10 -0
  70. package/build/database/migrations/1770000000049_print_templates.d.ts +5 -0
  71. package/build/database/migrations/1770000000049_print_templates.js +10 -0
  72. package/build/database/migrations/1770000000051_comment_notifications.d.ts +5 -0
  73. package/build/database/migrations/1770000000051_comment_notifications.js +10 -0
  74. package/build/database/migrations/1770000000054_cloud_backups.d.ts +5 -0
  75. package/build/database/migrations/1770000000054_cloud_backups.js +10 -0
  76. package/build/database/migrations/1770000000055_ai_assistant.d.ts +5 -0
  77. package/build/database/migrations/1770000000055_ai_assistant.js +10 -0
  78. package/build/database/migrations/1770000000056_appearance.d.ts +5 -0
  79. package/build/database/migrations/1770000000056_appearance.js +10 -0
  80. package/build/index.d.ts +87 -11
  81. package/build/index.js +50 -7
  82. package/build/providers/adula_provider.d.ts +2 -0
  83. package/build/providers/adula_provider.js +7 -0
  84. package/build/src/admin/contracts.js +1 -1
  85. package/build/src/admin/controller.d.ts +14 -1
  86. package/build/src/admin/controller.js +91 -13
  87. package/build/src/admin/errors.d.ts +32 -0
  88. package/build/src/admin/errors.js +63 -1
  89. package/build/src/admin/presentation.d.ts +21 -2
  90. package/build/src/admin/resource/editor.d.ts +60 -0
  91. package/build/src/admin/resource/editor.js +345 -0
  92. package/build/src/admin/resource/export.d.ts +54 -0
  93. package/build/src/admin/resource/export.js +262 -0
  94. package/build/src/admin/resource/lifecycle.d.ts +23 -0
  95. package/build/src/admin/resource/lifecycle.js +91 -0
  96. package/build/src/admin/resource/query.d.ts +111 -0
  97. package/build/src/admin/resource/query.js +595 -0
  98. package/build/src/admin/resource/read.d.ts +109 -0
  99. package/build/src/admin/resource/read.js +388 -0
  100. package/build/src/admin/resource/shared.d.ts +93 -0
  101. package/build/src/admin/resource/shared.js +265 -0
  102. package/build/src/admin/resource/trash.d.ts +10 -0
  103. package/build/src/admin/resource/trash.js +56 -0
  104. package/build/src/admin/resource/write.d.ts +57 -0
  105. package/build/src/admin/resource/write.js +603 -0
  106. package/build/src/admin/resource_service.d.ts +80 -193
  107. package/build/src/admin/resource_service.js +81 -1343
  108. package/build/src/admin/resource_tools.d.ts +111 -0
  109. package/build/src/admin/resource_tools.js +139 -0
  110. package/build/src/ai/assistant.d.ts +100 -0
  111. package/build/src/ai/assistant.js +487 -0
  112. package/build/src/ai/client.d.ts +49 -0
  113. package/build/src/ai/client.js +85 -0
  114. package/build/src/ai/prompt.d.ts +23 -0
  115. package/build/src/ai/prompt.js +83 -0
  116. package/build/src/ai/settings.d.ts +56 -0
  117. package/build/src/ai/settings.js +214 -0
  118. package/build/src/ai/usage.d.ts +16 -0
  119. package/build/src/ai/usage.js +33 -0
  120. package/build/src/appearance/appearance.d.ts +134 -0
  121. package/build/src/appearance/appearance.js +461 -0
  122. package/build/src/appearance/colors.d.ts +43 -0
  123. package/build/src/appearance/colors.js +99 -0
  124. package/build/src/appearance/logo.d.ts +32 -0
  125. package/build/src/appearance/logo.js +112 -0
  126. package/build/src/attachments/attachment_service.d.ts +46 -2
  127. package/build/src/attachments/attachment_service.js +96 -4
  128. package/build/src/auth/actor_store.d.ts +5 -1
  129. package/build/src/auth/actor_store.js +8 -4
  130. package/build/src/auth/conditions.d.ts +5 -0
  131. package/build/src/auth/conditions.js +26 -3
  132. package/build/src/auth/sql.js +17 -5
  133. package/build/src/auth/two_factor.d.ts +96 -0
  134. package/build/src/auth/two_factor.js +259 -0
  135. package/build/src/backups/archive.d.ts +52 -0
  136. package/build/src/backups/archive.js +362 -0
  137. package/build/src/backups/cloud_backups.d.ts +133 -0
  138. package/build/src/backups/cloud_backups.js +650 -0
  139. package/build/src/backups/doctor.d.ts +7 -0
  140. package/build/src/backups/doctor.js +55 -0
  141. package/build/src/backups/providers.d.ts +81 -0
  142. package/build/src/backups/providers.js +470 -0
  143. package/build/src/collaboration/assignments.d.ts +59 -2
  144. package/build/src/collaboration/assignments.js +196 -74
  145. package/build/src/collaboration/record_collaboration.d.ts +21 -4
  146. package/build/src/collaboration/record_collaboration.js +126 -51
  147. package/build/src/collaboration/shared.d.ts +24 -0
  148. package/build/src/collaboration/shared.js +34 -0
  149. package/build/src/commands/app_scaffold.d.ts +72 -0
  150. package/build/src/commands/app_scaffold.js +285 -0
  151. package/build/src/commands/capabilities.js +35 -9
  152. package/build/src/commands/doctor.d.ts +24 -2
  153. package/build/src/commands/doctor.js +81 -6
  154. package/build/src/commands/gap_report.d.ts +1 -1
  155. package/build/src/commands/gap_report.js +2 -2
  156. package/build/src/commands/generator.d.ts +37 -1
  157. package/build/src/commands/generator.js +61 -18
  158. package/build/src/commands/snapshot.d.ts +24 -13
  159. package/build/src/commands/snapshot.js +54 -14
  160. package/build/src/commands/source_markers.d.ts +10 -0
  161. package/build/src/commands/source_markers.js +29 -0
  162. package/build/src/core/activity.d.ts +2 -0
  163. package/build/src/core/activity.js +7 -1
  164. package/build/src/core/administrators.d.ts +38 -0
  165. package/build/src/core/administrators.js +145 -0
  166. package/build/src/core/avatar_image.d.ts +33 -0
  167. package/build/src/core/avatar_image.js +278 -0
  168. package/build/src/core/html_sanitizer.d.ts +20 -0
  169. package/build/src/core/html_sanitizer.js +261 -0
  170. package/build/src/core/like.d.ts +5 -0
  171. package/build/src/core/like.js +5 -0
  172. package/build/src/core/lookups.d.ts +80 -0
  173. package/build/src/core/lookups.js +319 -0
  174. package/build/src/core/mail_settings.d.ts +83 -0
  175. package/build/src/core/mail_settings.js +274 -0
  176. package/build/src/core/mail_test.d.ts +16 -0
  177. package/build/src/core/mail_test.js +81 -6
  178. package/build/src/core/message_templates.d.ts +15 -2
  179. package/build/src/core/message_templates.js +81 -11
  180. package/build/src/core/module_seed.js +2 -1
  181. package/build/src/core/notification_channels.d.ts +235 -0
  182. package/build/src/core/notification_channels.js +660 -0
  183. package/build/src/core/roles.js +26 -9
  184. package/build/src/core/saved_views.d.ts +8 -1
  185. package/build/src/core/saved_views.js +91 -26
  186. package/build/src/core/sequences.d.ts +32 -0
  187. package/build/src/core/sequences.js +97 -0
  188. package/build/src/core/settings.d.ts +5 -0
  189. package/build/src/core/settings.js +31 -3
  190. package/build/src/core/template_language.d.ts +61 -0
  191. package/build/src/core/template_language.js +141 -0
  192. package/build/src/core/ui_preferences.d.ts +10 -0
  193. package/build/src/core/ui_preferences.js +15 -1
  194. package/build/src/core/user_avatars.d.ts +79 -0
  195. package/build/src/core/user_avatars.js +186 -0
  196. package/build/src/core/user_invitations.d.ts +5 -1
  197. package/build/src/core/user_invitations.js +7 -1
  198. package/build/src/core/user_preferences.d.ts +36 -0
  199. package/build/src/core/user_preferences.js +125 -0
  200. package/build/src/core/users.d.ts +2 -0
  201. package/build/src/core/users.js +5 -3
  202. package/build/src/database/calendar_days.d.ts +19 -0
  203. package/build/src/database/calendar_days.js +36 -0
  204. package/build/src/database/schema.d.ts +201 -2
  205. package/build/src/database/schema.js +713 -59
  206. package/build/src/database/schema_diff.d.ts +70 -0
  207. package/build/src/database/schema_diff.js +190 -0
  208. package/build/src/events/record_mutation.d.ts +2 -1
  209. package/build/src/events/record_mutation.js +1 -0
  210. package/build/src/integrations/api_client.d.ts +37 -0
  211. package/build/src/integrations/api_client.js +567 -0
  212. package/build/src/integrations/import_adapters.d.ts +37 -0
  213. package/build/src/integrations/import_adapters.js +35 -0
  214. package/build/src/integrations/imports.d.ts +130 -16
  215. package/build/src/integrations/imports.js +434 -73
  216. package/build/src/integrations/openapi.d.ts +3 -1
  217. package/build/src/integrations/openapi.js +465 -31
  218. package/build/src/integrations/outbound.d.ts +94 -0
  219. package/build/src/integrations/outbound.js +276 -0
  220. package/build/src/integrations/print.d.ts +22 -1
  221. package/build/src/integrations/print.js +68 -22
  222. package/build/src/integrations/print_templates.d.ts +216 -0
  223. package/build/src/integrations/print_templates.js +1017 -0
  224. package/build/src/integrations/sync_jobs.d.ts +139 -0
  225. package/build/src/integrations/sync_jobs.js +416 -0
  226. package/build/src/integrations/webhooks.d.ts +12 -12
  227. package/build/src/integrations/webhooks.js +34 -43
  228. package/build/src/integrations/xlsx.d.ts +41 -0
  229. package/build/src/integrations/xlsx.js +519 -0
  230. package/build/src/languages/ar.d.ts +11 -0
  231. package/build/src/languages/ar.js +358 -0
  232. package/build/src/languages/ar_ui.d.ts +461 -0
  233. package/build/src/languages/ar_ui.js +464 -0
  234. package/build/src/languages/catalog.d.ts +66 -0
  235. package/build/src/languages/catalog.js +152 -0
  236. package/build/src/languages/en.d.ts +7 -0
  237. package/build/src/languages/en.js +352 -0
  238. package/build/src/languages/en_ui.d.ts +3 -0
  239. package/build/src/languages/en_ui.js +458 -0
  240. package/build/src/languages/validation.d.ts +26 -0
  241. package/build/src/languages/validation.js +60 -0
  242. package/build/src/lists/bulk_actions.d.ts +89 -0
  243. package/build/src/lists/bulk_actions.js +268 -0
  244. package/build/src/lists/dashboards.d.ts +138 -0
  245. package/build/src/lists/dashboards.js +290 -0
  246. package/build/src/mcp/tools.js +9 -104
  247. package/build/src/resource/define_resource.d.ts +18 -1
  248. package/build/src/resource/define_resource.js +212 -18
  249. package/build/src/resource/definition.d.ts +44 -0
  250. package/build/src/resource/definition.js +171 -0
  251. package/build/src/resource/registry.js +15 -4
  252. package/build/src/resource/types.d.ts +313 -16
  253. package/build/src/resource/values.d.ts +56 -1
  254. package/build/src/resource/values.js +237 -5
  255. package/build/src/services/settings.d.ts +2 -0
  256. package/build/src/services/settings.js +5 -1
  257. package/build/src/upgrade/legacy.js +2 -2
  258. package/build/src/workflows/calendar.d.ts +9 -0
  259. package/build/src/workflows/calendar.js +44 -0
  260. package/build/src/workflows/define_workflow.d.ts +48 -3
  261. package/build/src/workflows/define_workflow.js +57 -13
  262. package/build/src/workflows/delegations.d.ts +107 -0
  263. package/build/src/workflows/delegations.js +326 -0
  264. package/build/src/workflows/engine.d.ts +80 -4
  265. package/build/src/workflows/engine.js +466 -86
  266. package/build/src/workflows/sla.d.ts +15 -0
  267. package/build/src/workflows/sla.js +8 -0
  268. package/build/stubs/resource_contract.txt +12 -1
  269. package/package.json +9 -2
@@ -0,0 +1,111 @@
1
+ import type { ResourceRuntime } from './controller.js';
2
+ import type { ListOptions } from './resource_service.js';
3
+ /**
4
+ * The resource tool surface shared by MCP and the built-in assistant: one argument
5
+ * parser, one dispatcher and one error mapping, so both reach ResourceService through
6
+ * the same path. Nothing here imports the optional MCP package.
7
+ */
8
+ export type ResourceToolAction = 'list' | 'show' | 'editor' | 'create' | 'update' | 'delete' | 'submit' | 'cancel';
9
+ export type ResourceToolCall = {
10
+ write: boolean;
11
+ resource: string;
12
+ action: ResourceToolAction;
13
+ id?: number;
14
+ options?: ListOptions;
15
+ input?: Record<string, unknown>;
16
+ version?: unknown;
17
+ };
18
+ export type ResourceToolError = {
19
+ status: number;
20
+ code: string;
21
+ message?: string;
22
+ messages?: unknown;
23
+ };
24
+ /**
25
+ * Validates tool arguments against a fixed whitelist. The actor never comes from the
26
+ * arguments: unknown keys (an `actorId`, a role) are refused.
27
+ */
28
+ export declare function parseResourceToolArgs(runtime: Pick<ResourceRuntime, 'registry'>, value: unknown, write: boolean): ResourceToolCall;
29
+ /** Runs a parsed call through ResourceService as `runtime.actor`. */
30
+ export declare function runResourceTool(runtime: ResourceRuntime, call: ResourceToolCall): Promise<unknown>;
31
+ /** Maps a failure to a wire-safe error: no SQL, credentials or stack traces. */
32
+ export declare function resourceToolError(error: unknown): ResourceToolError;
33
+ /** JSON Schema of the read tool's arguments. */
34
+ export declare const RESOURCE_READ_SCHEMA: {
35
+ readonly type: "object";
36
+ readonly additionalProperties: false;
37
+ readonly properties: {
38
+ readonly resource: {
39
+ readonly type: "string";
40
+ };
41
+ readonly action: {
42
+ readonly type: "string";
43
+ readonly enum: readonly ["list", "show", "editor"];
44
+ };
45
+ readonly id: {
46
+ readonly type: "integer";
47
+ readonly minimum: 1;
48
+ };
49
+ readonly options: {
50
+ readonly type: "object";
51
+ readonly properties: {
52
+ readonly limit: {
53
+ readonly type: "integer";
54
+ readonly minimum: 1;
55
+ readonly maximum: 100;
56
+ };
57
+ readonly cursor: {
58
+ readonly type: "string";
59
+ };
60
+ readonly search: {
61
+ readonly type: "string";
62
+ };
63
+ readonly sort: {
64
+ readonly type: "string";
65
+ };
66
+ readonly direction: {
67
+ readonly type: "string";
68
+ readonly enum: readonly ["asc", "desc"];
69
+ };
70
+ readonly filters: {
71
+ readonly type: "object";
72
+ readonly properties: {};
73
+ readonly additionalProperties: true;
74
+ };
75
+ readonly estimate: {
76
+ readonly type: "boolean";
77
+ };
78
+ };
79
+ readonly additionalProperties: false;
80
+ };
81
+ };
82
+ readonly required: readonly ["resource", "action"];
83
+ };
84
+ /** JSON Schema of the write tool's arguments. */
85
+ export declare const RESOURCE_WRITE_SCHEMA: {
86
+ readonly type: "object";
87
+ readonly additionalProperties: false;
88
+ readonly properties: {
89
+ readonly resource: {
90
+ readonly type: "string";
91
+ };
92
+ readonly action: {
93
+ readonly type: "string";
94
+ readonly enum: readonly ["create", "update", "delete", "submit", "cancel"];
95
+ };
96
+ readonly id: {
97
+ readonly type: "integer";
98
+ readonly minimum: 1;
99
+ };
100
+ readonly input: {
101
+ readonly type: "object";
102
+ readonly properties: {};
103
+ readonly additionalProperties: true;
104
+ };
105
+ readonly version: {
106
+ readonly type: "integer";
107
+ readonly minimum: 1;
108
+ };
109
+ };
110
+ readonly required: readonly ["resource", "action"];
111
+ };
@@ -0,0 +1,139 @@
1
+ import { AdulaError } from './errors.js';
2
+ const READ_KEYS = ['resource', 'action', 'id', 'options'];
3
+ const WRITE_KEYS = ['resource', 'action', 'id', 'input', 'version'];
4
+ const LIST_KEYS = ['limit', 'cursor', 'search', 'sort', 'direction', 'filters', 'estimate'];
5
+ function object(value) {
6
+ if (!value || typeof value !== 'object' || Array.isArray(value))
7
+ throw new AdulaError(422, 'E_INPUT', 'Expected an object');
8
+ return value;
9
+ }
10
+ function recordId(value) {
11
+ if (!Number.isSafeInteger(value) || Number(value) < 1)
12
+ throw new AdulaError(422, 'E_ID', 'Expected a positive integer record ID');
13
+ return Number(value);
14
+ }
15
+ /**
16
+ * Validates tool arguments against a fixed whitelist. The actor never comes from the
17
+ * arguments: unknown keys (an `actorId`, a role) are refused.
18
+ */
19
+ export function parseResourceToolArgs(runtime, value, write) {
20
+ const args = object(value);
21
+ const accepted = write ? WRITE_KEYS : READ_KEYS;
22
+ if (Object.keys(args).some((key) => !accepted.includes(key)))
23
+ throw new AdulaError(422, 'E_INPUT', 'Unknown tool argument');
24
+ if (typeof args.resource !== 'string' ||
25
+ !runtime.registry.all().some((r) => r.name === args.resource))
26
+ throw new AdulaError(404, 'E_NOT_FOUND', 'الكيان غير موجود');
27
+ const resource = args.resource;
28
+ if (!write && args.action === 'list') {
29
+ const options = args.options === undefined ? {} : object(args.options);
30
+ if (Object.keys(options).some((key) => !LIST_KEYS.includes(key)))
31
+ throw new AdulaError(422, 'E_INPUT', 'Unknown list option');
32
+ for (const key of ['cursor', 'search', 'sort'])
33
+ if (options[key] !== undefined && typeof options[key] !== 'string')
34
+ throw new AdulaError(422, 'E_INPUT', `Invalid ${key}`);
35
+ if ((options.limit !== undefined && !Number.isSafeInteger(options.limit)) ||
36
+ (options.direction !== undefined && !['asc', 'desc'].includes(String(options.direction))) ||
37
+ (options.estimate !== undefined && typeof options.estimate !== 'boolean'))
38
+ throw new AdulaError(422, 'E_INPUT', 'Invalid list options');
39
+ if (options.filters !== undefined)
40
+ object(options.filters);
41
+ return { write, resource, action: 'list', options: options };
42
+ }
43
+ if (!write && args.action === 'show')
44
+ return { write, resource, action: 'show', id: recordId(args.id) };
45
+ if (!write && args.action === 'editor')
46
+ return {
47
+ write,
48
+ resource,
49
+ action: 'editor',
50
+ ...(args.id === undefined ? {} : { id: recordId(args.id) }),
51
+ };
52
+ 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
+ return {
56
+ write,
57
+ resource,
58
+ action: args.action,
59
+ input: object(args.input),
60
+ ...(args.action === 'update' ? { id: recordId(args.id) } : {}),
61
+ };
62
+ }
63
+ if (write && (args.action === 'delete' || args.action === 'submit' || args.action === 'cancel'))
64
+ return { write, resource, action: args.action, id: recordId(args.id), version: args.version };
65
+ throw new AdulaError(422, 'E_ACTION', 'Unsupported resource action');
66
+ }
67
+ /** Runs a parsed call through ResourceService as `runtime.actor`. */
68
+ export async function runResourceTool(runtime, call) {
69
+ const { resources, actor } = runtime;
70
+ const name = call.resource;
71
+ switch (call.action) {
72
+ case 'list':
73
+ return resources.list(name, actor, call.options ?? {});
74
+ case 'show':
75
+ return resources.show(name, call.id, actor);
76
+ case 'editor':
77
+ return resources.editor(name, actor, call.id);
78
+ case 'create':
79
+ case 'update':
80
+ return {
81
+ data: await resources.save(name, actor, call.input ?? {}, call.action === 'update' ? call.id : undefined),
82
+ };
83
+ default:
84
+ return {
85
+ data: await resources.transition(name, call.id, actor, call.action, call.version),
86
+ };
87
+ }
88
+ }
89
+ /** Maps a failure to a wire-safe error: no SQL, credentials or stack traces. */
90
+ export function resourceToolError(error) {
91
+ if (error instanceof AdulaError)
92
+ return { status: error.status, code: error.code, message: error.message };
93
+ if (error?.code === 'E_VALIDATION_ERROR')
94
+ return {
95
+ status: 422,
96
+ code: 'E_VALIDATION_ERROR',
97
+ messages: error.messages,
98
+ };
99
+ if (['23505', '23503', '23502'].includes(error?.code ?? ''))
100
+ return { status: 422, code: 'E_CONSTRAINT', message: 'تحقق من القيم والسجلات المرتبطة' };
101
+ return { status: 500, code: 'E_INTERNAL', message: 'تعذر إتمام العملية' };
102
+ }
103
+ /** JSON Schema of the read tool's arguments. */
104
+ export const RESOURCE_READ_SCHEMA = {
105
+ type: 'object',
106
+ additionalProperties: false,
107
+ properties: {
108
+ resource: { type: 'string' },
109
+ action: { type: 'string', enum: ['list', 'show', 'editor'] },
110
+ id: { type: 'integer', minimum: 1 },
111
+ options: {
112
+ type: 'object',
113
+ properties: {
114
+ limit: { type: 'integer', minimum: 1, maximum: 100 },
115
+ cursor: { type: 'string' },
116
+ search: { type: 'string' },
117
+ sort: { type: 'string' },
118
+ direction: { type: 'string', enum: ['asc', 'desc'] },
119
+ filters: { type: 'object', properties: {}, additionalProperties: true },
120
+ estimate: { type: 'boolean' },
121
+ },
122
+ additionalProperties: false,
123
+ },
124
+ },
125
+ required: ['resource', 'action'],
126
+ };
127
+ /** JSON Schema of the write tool's arguments. */
128
+ export const RESOURCE_WRITE_SCHEMA = {
129
+ type: 'object',
130
+ additionalProperties: false,
131
+ properties: {
132
+ resource: { type: 'string' },
133
+ action: { type: 'string', enum: ['create', 'update', 'delete', 'submit', 'cancel'] },
134
+ id: { type: 'integer', minimum: 1 },
135
+ input: { type: 'object', properties: {}, additionalProperties: true },
136
+ version: { type: 'integer', minimum: 1 },
137
+ },
138
+ required: ['resource', 'action'],
139
+ };
@@ -0,0 +1,100 @@
1
+ import type { Knex } from 'knex';
2
+ import type { Actor } from '../auth/ability.js';
3
+ import type { ResourceRegistry } from '../resource/registry.js';
4
+ import type { ResourceService } from '../admin/resource_service.js';
5
+ import { type ChatClient } from './client.js';
6
+ import { type AssistantSettings } from './settings.js';
7
+ export declare const ASSISTANT_SUBJECT = "core.ai";
8
+ export declare const MESSAGE_LIMIT = 4000;
9
+ export declare const HISTORY_ENTRIES = 20;
10
+ export declare const HISTORY_CHARACTERS = 24000;
11
+ export declare const LIST_ROW_LIMIT = 20;
12
+ export declare const PROPOSAL_TTL_MINUTES = 10;
13
+ export type AssistantProposal = {
14
+ id: string;
15
+ resource: string;
16
+ resourceLabel: string;
17
+ action: string;
18
+ recordId: number | null;
19
+ recordTitle: string | null;
20
+ input: Record<string, unknown> | null;
21
+ before: Record<string, unknown> | null;
22
+ version: number | null;
23
+ expiresAt: string;
24
+ };
25
+ export type AssistantTurnResult = {
26
+ reply: string;
27
+ proposals: AssistantProposal[];
28
+ usage: {
29
+ remainingToday: number;
30
+ };
31
+ };
32
+ export type AssistantStatus = {
33
+ available: boolean;
34
+ noticeAccepted: boolean;
35
+ };
36
+ type HistoryItem = {
37
+ role: 'user' | 'assistant';
38
+ content: string;
39
+ };
40
+ /** Validates a turn body: only the new message and prior user/assistant text. */
41
+ export declare function parseTurnInput(value: unknown): {
42
+ message: string;
43
+ history: HistoryItem[];
44
+ };
45
+ /**
46
+ * The built-in assistant (ADR 052). Every tool call runs through the shared resource tool
47
+ * runner as the signed-in actor; the model can only propose writes, which run when the
48
+ * user confirms them in a separate request.
49
+ */
50
+ export declare class Assistant {
51
+ private db;
52
+ private registry;
53
+ private resources;
54
+ private settings;
55
+ private client;
56
+ private options;
57
+ constructor(db: Knex, registry: ResourceRegistry, resources: ResourceService, settings: AssistantSettings, client: ChatClient, options?: {
58
+ now?: () => Date;
59
+ });
60
+ private now;
61
+ /** Whether the role rules grant `use` on `core.ai` (administrators always pass). */
62
+ canUse(actor: Actor): boolean;
63
+ status(actor: Actor): Promise<AssistantStatus>;
64
+ private noticeAccepted;
65
+ /** Permission, configuration and key; the provider key is returned for this call only. */
66
+ private usable;
67
+ /** Records that the user read the provider notice; an impersonator cannot accept it. */
68
+ acceptNotice(actor: Actor): Promise<void>;
69
+ startConversation(actor: Actor): Promise<{
70
+ id: number;
71
+ }>;
72
+ turn(actor: Actor, conversationId: number, body: unknown): Promise<AssistantTurnResult>;
73
+ private conversation;
74
+ private runtime;
75
+ private audit;
76
+ /**
77
+ * Runs one model tool call. Names, argument whitelist and the actor are fixed here;
78
+ * nothing in the arguments or in earlier tool output can widen them.
79
+ */
80
+ private runTool;
81
+ /** Stores a proposal for the user to confirm. No ResourceService write happens here. */
82
+ private propose;
83
+ /** Atomically takes the user's own unexpired proposal; null when there is none. */
84
+ private take;
85
+ /** Runs a proposal the user confirmed, through ResourceService as the user. */
86
+ confirm(actor: Actor, conversationId: number, id: string): Promise<unknown>;
87
+ reject(actor: Actor, conversationId: number, id: string): Promise<void>;
88
+ /** Sends one minimal request with the stored key; returns only a failure category. */
89
+ testConnection(): Promise<{
90
+ ok: boolean;
91
+ category: string | null;
92
+ }>;
93
+ /** Retention: conversation metadata after 30 days, used proposals after a day, usage after 90. */
94
+ prune(now?: Date): Promise<{
95
+ conversations: number;
96
+ proposals: number;
97
+ usage: number;
98
+ }>;
99
+ }
100
+ export {};