@cldmv/slothlet 2.11.0 → 3.0.1

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 (189) hide show
  1. package/AGENT-USAGE.md +355 -325
  2. package/README.md +554 -238
  3. package/dist/lib/builders/api-assignment.mjs +605 -0
  4. package/dist/lib/builders/api_builder.mjs +1073 -0
  5. package/dist/lib/builders/builder.mjs +94 -0
  6. package/dist/lib/builders/modes-processor.mjs +1816 -0
  7. package/dist/lib/errors.mjs +227 -0
  8. package/dist/lib/factories/component-base.mjs +96 -0
  9. package/dist/lib/factories/context.mjs +38 -0
  10. package/dist/lib/handlers/api-cache-manager.mjs +216 -0
  11. package/dist/lib/handlers/api-manager.mjs +2364 -0
  12. package/dist/lib/handlers/context-async.mjs +184 -0
  13. package/dist/lib/handlers/context-live.mjs +184 -0
  14. package/dist/lib/handlers/hook-manager.mjs +789 -0
  15. package/dist/lib/handlers/lifecycle-token.mjs +44 -0
  16. package/dist/lib/handlers/lifecycle.mjs +131 -0
  17. package/dist/lib/handlers/materialize-manager.mjs +64 -0
  18. package/dist/lib/handlers/metadata.mjs +500 -0
  19. package/dist/lib/handlers/ownership.mjs +338 -0
  20. package/dist/lib/handlers/unified-wrapper.mjs +3031 -0
  21. package/dist/lib/helpers/class-instance-wrapper.mjs +125 -0
  22. package/dist/lib/helpers/config.mjs +343 -0
  23. package/dist/lib/helpers/eventemitter-context.mjs +365 -0
  24. package/dist/lib/helpers/hint-detector.mjs +63 -0
  25. package/dist/lib/helpers/modes-utils.mjs +53 -0
  26. package/dist/lib/helpers/resolve-from-caller.mjs +123 -117
  27. package/dist/lib/helpers/sanitize.mjs +247 -168
  28. package/dist/lib/helpers/utilities.mjs +46 -81
  29. package/dist/lib/i18n/languages/de-de.json +377 -0
  30. package/dist/lib/i18n/languages/en-gb.json +377 -0
  31. package/dist/lib/i18n/languages/en-us.json +377 -0
  32. package/dist/lib/i18n/languages/es-mx.json +377 -0
  33. package/dist/lib/i18n/languages/fr-fr.json +377 -0
  34. package/dist/lib/i18n/languages/hi-in.json +377 -0
  35. package/dist/lib/i18n/languages/ja-jp.json +377 -0
  36. package/dist/lib/i18n/languages/ko-kr.json +377 -0
  37. package/dist/lib/i18n/languages/pt-br.json +377 -0
  38. package/dist/lib/i18n/languages/ru-ru.json +377 -0
  39. package/dist/lib/i18n/languages/zh-cn.json +377 -0
  40. package/dist/lib/i18n/translations.mjs +140 -0
  41. package/dist/lib/modes/eager.mjs +75 -0
  42. package/dist/lib/modes/lazy.mjs +97 -0
  43. package/dist/lib/processors/flatten.mjs +453 -0
  44. package/dist/lib/processors/loader.mjs +355 -0
  45. package/dist/lib/processors/type-generator.mjs +291 -0
  46. package/dist/lib/processors/typescript.mjs +188 -0
  47. package/dist/lib/runtime/runtime-asynclocalstorage.mjs +80 -522
  48. package/dist/lib/runtime/runtime-livebindings.mjs +45 -390
  49. package/dist/lib/runtime/runtime.mjs +39 -159
  50. package/dist/slothlet.mjs +525 -744
  51. package/docs/API-RULES.md +338 -486
  52. package/index.cjs +4 -4
  53. package/index.mjs +82 -45
  54. package/package.json +143 -30
  55. package/types/dist/lib/builders/api-assignment.d.mts +97 -0
  56. package/types/dist/lib/builders/api-assignment.d.mts.map +1 -0
  57. package/types/dist/lib/builders/api_builder.d.mts +96 -0
  58. package/types/dist/lib/builders/api_builder.d.mts.map +1 -0
  59. package/types/dist/lib/builders/builder.d.mts +60 -0
  60. package/types/dist/lib/builders/builder.d.mts.map +1 -0
  61. package/types/dist/lib/builders/modes-processor.d.mts +32 -0
  62. package/types/dist/lib/builders/modes-processor.d.mts.map +1 -0
  63. package/types/dist/lib/errors.d.mts +118 -0
  64. package/types/dist/lib/errors.d.mts.map +1 -0
  65. package/types/dist/lib/factories/component-base.d.mts +182 -0
  66. package/types/dist/lib/factories/component-base.d.mts.map +1 -0
  67. package/types/dist/lib/factories/context.d.mts +26 -0
  68. package/types/dist/lib/factories/context.d.mts.map +1 -0
  69. package/types/dist/lib/handlers/api-cache-manager.d.mts +208 -0
  70. package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -0
  71. package/types/dist/lib/handlers/api-manager.d.mts +392 -0
  72. package/types/dist/lib/handlers/api-manager.d.mts.map +1 -0
  73. package/types/dist/lib/handlers/context-async.d.mts +66 -0
  74. package/types/dist/lib/handlers/context-async.d.mts.map +1 -0
  75. package/types/dist/lib/handlers/context-live.d.mts +65 -0
  76. package/types/dist/lib/handlers/context-live.d.mts.map +1 -0
  77. package/types/dist/lib/handlers/hook-manager.d.mts +199 -0
  78. package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -0
  79. package/types/dist/lib/handlers/lifecycle-token.d.mts +49 -0
  80. package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -0
  81. package/types/dist/lib/handlers/lifecycle.d.mts +90 -0
  82. package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -0
  83. package/types/dist/lib/handlers/materialize-manager.d.mts +75 -0
  84. package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -0
  85. package/types/dist/lib/handlers/metadata.d.mts +215 -0
  86. package/types/dist/lib/handlers/metadata.d.mts.map +1 -0
  87. package/types/dist/lib/handlers/ownership.d.mts +170 -0
  88. package/types/dist/lib/handlers/ownership.d.mts.map +1 -0
  89. package/types/dist/lib/handlers/unified-wrapper.d.mts +250 -0
  90. package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -0
  91. package/types/dist/lib/helpers/class-instance-wrapper.d.mts +54 -0
  92. package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -0
  93. package/types/dist/lib/helpers/config.d.mts +96 -0
  94. package/types/dist/lib/helpers/config.d.mts.map +1 -0
  95. package/types/dist/lib/helpers/eventemitter-context.d.mts +31 -0
  96. package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -0
  97. package/types/dist/lib/helpers/hint-detector.d.mts +20 -0
  98. package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -0
  99. package/types/dist/lib/helpers/modes-utils.d.mts +35 -0
  100. package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -0
  101. package/types/dist/lib/helpers/resolve-from-caller.d.mts +29 -145
  102. package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
  103. package/types/dist/lib/helpers/sanitize.d.mts +95 -94
  104. package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
  105. package/types/dist/lib/helpers/utilities.d.mts +53 -116
  106. package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
  107. package/types/dist/lib/i18n/translations.d.mts +39 -0
  108. package/types/dist/lib/i18n/translations.d.mts.map +1 -0
  109. package/types/dist/lib/modes/eager.d.mts +36 -0
  110. package/types/dist/lib/modes/eager.d.mts.map +1 -0
  111. package/types/dist/lib/modes/lazy.d.mts +49 -0
  112. package/types/dist/lib/modes/lazy.d.mts.map +1 -0
  113. package/types/dist/lib/processors/flatten.d.mts +114 -0
  114. package/types/dist/lib/processors/flatten.d.mts.map +1 -0
  115. package/types/dist/lib/processors/loader.d.mts +47 -0
  116. package/types/dist/lib/processors/loader.d.mts.map +1 -0
  117. package/types/dist/lib/processors/type-generator.d.mts +19 -0
  118. package/types/dist/lib/processors/type-generator.d.mts.map +1 -0
  119. package/types/dist/lib/processors/typescript.d.mts +55 -0
  120. package/types/dist/lib/processors/typescript.d.mts.map +1 -0
  121. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +47 -42
  122. package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
  123. package/types/dist/lib/runtime/runtime-livebindings.d.mts +34 -65
  124. package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
  125. package/types/dist/lib/runtime/runtime.d.mts +39 -9
  126. package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
  127. package/types/dist/slothlet.d.mts +184 -111
  128. package/types/dist/slothlet.d.mts.map +1 -1
  129. package/types/index.d.mts +1 -3
  130. package/dist/lib/engine/README.md +0 -21
  131. package/dist/lib/engine/slothlet_child.mjs +0 -59
  132. package/dist/lib/engine/slothlet_engine.mjs +0 -372
  133. package/dist/lib/engine/slothlet_esm.mjs +0 -230
  134. package/dist/lib/engine/slothlet_helpers.mjs +0 -455
  135. package/dist/lib/engine/slothlet_worker.mjs +0 -149
  136. package/dist/lib/helpers/als-eventemitter.mjs +0 -256
  137. package/dist/lib/helpers/api_builder/add_api.mjs +0 -553
  138. package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
  139. package/dist/lib/helpers/api_builder/construction.mjs +0 -495
  140. package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
  141. package/dist/lib/helpers/api_builder/metadata.mjs +0 -248
  142. package/dist/lib/helpers/api_builder.mjs +0 -41
  143. package/dist/lib/helpers/auto-wrap.mjs +0 -62
  144. package/dist/lib/helpers/hooks.mjs +0 -389
  145. package/dist/lib/helpers/instance-manager.mjs +0 -111
  146. package/dist/lib/helpers/metadata-api.mjs +0 -201
  147. package/dist/lib/helpers/multidefault.mjs +0 -216
  148. package/dist/lib/modes/slothlet_eager.mjs +0 -154
  149. package/dist/lib/modes/slothlet_lazy.mjs +0 -594
  150. package/docs/API-RULES-CONDITIONS.md +0 -712
  151. package/types/dist/lib/engine/slothlet_child.d.mts +0 -2
  152. package/types/dist/lib/engine/slothlet_child.d.mts.map +0 -1
  153. package/types/dist/lib/engine/slothlet_engine.d.mts +0 -31
  154. package/types/dist/lib/engine/slothlet_engine.d.mts.map +0 -1
  155. package/types/dist/lib/engine/slothlet_esm.d.mts +0 -19
  156. package/types/dist/lib/engine/slothlet_esm.d.mts.map +0 -1
  157. package/types/dist/lib/engine/slothlet_helpers.d.mts +0 -25
  158. package/types/dist/lib/engine/slothlet_helpers.d.mts.map +0 -1
  159. package/types/dist/lib/engine/slothlet_worker.d.mts +0 -2
  160. package/types/dist/lib/engine/slothlet_worker.d.mts.map +0 -1
  161. package/types/dist/lib/helpers/als-eventemitter.d.mts +0 -56
  162. package/types/dist/lib/helpers/als-eventemitter.d.mts.map +0 -1
  163. package/types/dist/lib/helpers/api_builder/add_api.d.mts +0 -102
  164. package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +0 -1
  165. package/types/dist/lib/helpers/api_builder/analysis.d.mts +0 -189
  166. package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +0 -1
  167. package/types/dist/lib/helpers/api_builder/construction.d.mts +0 -107
  168. package/types/dist/lib/helpers/api_builder/construction.d.mts.map +0 -1
  169. package/types/dist/lib/helpers/api_builder/decisions.d.mts +0 -213
  170. package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +0 -1
  171. package/types/dist/lib/helpers/api_builder/metadata.d.mts +0 -99
  172. package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +0 -1
  173. package/types/dist/lib/helpers/api_builder.d.mts +0 -6
  174. package/types/dist/lib/helpers/api_builder.d.mts.map +0 -1
  175. package/types/dist/lib/helpers/auto-wrap.d.mts +0 -49
  176. package/types/dist/lib/helpers/auto-wrap.d.mts.map +0 -1
  177. package/types/dist/lib/helpers/hooks.d.mts +0 -342
  178. package/types/dist/lib/helpers/hooks.d.mts.map +0 -1
  179. package/types/dist/lib/helpers/instance-manager.d.mts +0 -41
  180. package/types/dist/lib/helpers/instance-manager.d.mts.map +0 -1
  181. package/types/dist/lib/helpers/metadata-api.d.mts +0 -132
  182. package/types/dist/lib/helpers/metadata-api.d.mts.map +0 -1
  183. package/types/dist/lib/helpers/multidefault.d.mts +0 -90
  184. package/types/dist/lib/helpers/multidefault.d.mts.map +0 -1
  185. package/types/dist/lib/modes/slothlet_eager.d.mts +0 -65
  186. package/types/dist/lib/modes/slothlet_eager.d.mts.map +0 -1
  187. package/types/dist/lib/modes/slothlet_lazy.d.mts +0 -31
  188. package/types/dist/lib/modes/slothlet_lazy.d.mts.map +0 -1
  189. package/types/index.d.mts.map +0 -1
@@ -0,0 +1,789 @@
1
+ /*
2
+ Copyright 2026 CLDMV/Shinrai
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+
18
+
19
+
20
+
21
+ import { ComponentBase } from "@cldmv/slothlet/factories/component-base";
22
+
23
+
24
+
25
+
26
+ const ERROR_HOOK_PROCESSED = Symbol.for("@cldmv/slothlet/hook-error-processed");
27
+
28
+
29
+ export class HookManager extends ComponentBase {
30
+
31
+ static slothletProperty = "hookManager";
32
+
33
+
34
+ #hooks = {
35
+ before: { before: {}, primary: {}, after: {} },
36
+ after: { before: {}, primary: {}, after: {} },
37
+ always: { before: {}, primary: {}, after: {} },
38
+ error: { before: {}, primary: {}, after: {} }
39
+ };
40
+
41
+
42
+ #byId = new Map();
43
+
44
+
45
+ #idCounter = 0;
46
+
47
+
48
+ #validTypes = new Set(["before", "after", "always", "error"]);
49
+
50
+
51
+ #validSubsets = new Set(["before", "primary", "after"]);
52
+
53
+
54
+ constructor(slothlet) {
55
+ super(slothlet);
56
+
57
+
58
+ const hookConfig = slothlet.config?.hook || { enabled: false, pattern: "**", suppressErrors: false };
59
+
60
+ this.enabled = hookConfig.enabled;
61
+ this.defaultPattern = hookConfig.pattern || "**";
62
+ this.suppressErrors = hookConfig.suppressErrors || false;
63
+ this.enabledPatterns = new Set();
64
+ this.patternFilterActive = false;
65
+
66
+ this.hooks = new Map();
67
+ this.registrationOrder = 0;
68
+ this.reportedErrors = new WeakSet();
69
+ }
70
+
71
+
72
+ on(typePattern, handler, options = {}) {
73
+
74
+ let { type, pattern } = this.#parseTypePattern(typePattern);
75
+
76
+
77
+ if (options.pattern !== undefined) {
78
+ pattern = options.pattern;
79
+ }
80
+
81
+
82
+ if (!this.#validTypes.has(type)) {
83
+ throw new this.slothlet.SlothletError("INVALID_HOOK_TYPE", {
84
+ type,
85
+ validTypes: Array.from(this.#validTypes)
86
+ });
87
+ }
88
+
89
+
90
+ if (typeof handler !== "function") {
91
+ throw new this.slothlet.SlothletError("INVALID_HOOK_HANDLER", {
92
+ receivedType: typeof handler,
93
+ validationError: true
94
+ });
95
+ }
96
+
97
+
98
+ const id = options.id || this.#generateId();
99
+
100
+
101
+ if (this.#byId.has(id)) {
102
+ throw new this.slothlet.SlothletError("DUPLICATE_HOOK_ID", { id, validationError: true });
103
+ }
104
+
105
+
106
+ const subset = options.subset || "primary";
107
+ if (!this.#validSubsets.has(subset)) {
108
+ throw new this.slothlet.SlothletError("INVALID_HOOK_SUBSET", {
109
+ subset,
110
+ validSubsets: Array.from(this.#validSubsets)
111
+ });
112
+ }
113
+
114
+
115
+ this.#compilePattern(pattern);
116
+
117
+
118
+ const hook = {
119
+ id,
120
+ type,
121
+ pattern,
122
+ handler,
123
+ priority: options.priority || 0,
124
+ subset,
125
+ enabled: true,
126
+ _compiled: null
127
+ };
128
+
129
+
130
+ const typeIndex = this.#hooks[type];
131
+ const subsetIndex = typeIndex[subset];
132
+
133
+ if (!subsetIndex[pattern]) {
134
+ subsetIndex[pattern] = [];
135
+ }
136
+ subsetIndex[pattern].push(hook);
137
+
138
+
139
+ this.#byId.set(id, hook);
140
+
141
+ return id;
142
+ }
143
+
144
+
145
+ remove(filter = {}) {
146
+ let removed = 0;
147
+
148
+
149
+ if (filter.id) {
150
+ const hook = this.#byId.get(filter.id);
151
+ if (hook) {
152
+ this.#removeHook(hook);
153
+ removed = 1;
154
+ }
155
+ return removed;
156
+ }
157
+
158
+
159
+ const types = filter.type ? [filter.type] : Array.from(this.#validTypes);
160
+
161
+ for (const type of types) {
162
+ const typeIndex = this.#hooks[type];
163
+
164
+ for (const subset of ["before", "primary", "after"]) {
165
+ const subsetIndex = typeIndex[subset];
166
+
167
+
168
+ if (filter.pattern) {
169
+ const patternHooks = subsetIndex[filter.pattern];
170
+ if (patternHooks) {
171
+ removed += patternHooks.length;
172
+ patternHooks.forEach((hook) => this.#byId.delete(hook.id));
173
+ delete subsetIndex[filter.pattern];
174
+ }
175
+ } else {
176
+
177
+ for (const pattern in subsetIndex) {
178
+ const patternHooks = subsetIndex[pattern];
179
+ removed += patternHooks.length;
180
+ patternHooks.forEach((hook) => this.#byId.delete(hook.id));
181
+ delete subsetIndex[pattern];
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ return removed;
188
+ }
189
+
190
+
191
+ enable(filter = {}) {
192
+
193
+ if (typeof filter === "string") {
194
+ filter = { pattern: filter };
195
+ }
196
+
197
+
198
+ this.enabled = true;
199
+
200
+ return this.#setEnabledState(filter, true);
201
+ }
202
+
203
+
204
+ disable(filter = {}) {
205
+
206
+ if (typeof filter === "string") {
207
+ filter = { pattern: filter };
208
+ }
209
+
210
+
211
+ if (Object.keys(filter).length === 0) {
212
+ this.enabled = false;
213
+ }
214
+ return this.#setEnabledState(filter, false);
215
+ }
216
+
217
+
218
+ list(filter = {}) {
219
+
220
+ if (typeof filter === "string") {
221
+
222
+ if (this.#validTypes.has(filter)) {
223
+ filter = { type: filter };
224
+ } else {
225
+
226
+ filter = { pattern: filter };
227
+ }
228
+ }
229
+
230
+ const hooks = [];
231
+
232
+
233
+ if (filter.id) {
234
+ const hook = this.#byId.get(filter.id);
235
+ if (hook && (filter.enabled === undefined || hook.enabled === filter.enabled)) {
236
+ hooks.push(this.#serializeHook(hook));
237
+ }
238
+ return { registeredHooks: hooks };
239
+ }
240
+
241
+
242
+ const types = filter.type ? [filter.type] : Array.from(this.#validTypes);
243
+
244
+
245
+ let patternMatcher = null;
246
+ if (filter.pattern) {
247
+ patternMatcher = this.#compilePattern(filter.pattern);
248
+ }
249
+
250
+ for (const type of types) {
251
+ const typeIndex = this.#hooks[type];
252
+
253
+ for (const subset of ["before", "primary", "after"]) {
254
+ const subsetIndex = typeIndex[subset];
255
+
256
+
257
+ for (const pattern in subsetIndex) {
258
+ const patternHooks = subsetIndex[pattern];
259
+ for (const hook of patternHooks) {
260
+
261
+ if (filter.enabled !== undefined && hook.enabled !== filter.enabled) {
262
+ continue;
263
+ }
264
+
265
+
266
+ if (patternMatcher && !patternMatcher(hook.pattern)) {
267
+ continue;
268
+ }
269
+
270
+ hooks.push(this.#serializeHook(hook));
271
+ }
272
+ }
273
+ }
274
+ }
275
+
276
+ return { registeredHooks: hooks };
277
+ }
278
+
279
+
280
+ getHooksForPath(type, apiPath) {
281
+
282
+ if (this.enabled === false) {
283
+ return [];
284
+ }
285
+
286
+ const typeIndex = this.#hooks[type];
287
+ if (!typeIndex) {
288
+ return [];
289
+ }
290
+
291
+ const hooks = [];
292
+
293
+
294
+ for (const subset of ["before", "primary", "after"]) {
295
+ const subsetIndex = typeIndex[subset];
296
+ const subsetHooks = [];
297
+
298
+
299
+ for (const pattern in subsetIndex) {
300
+ const patternHooks = subsetIndex[pattern];
301
+
302
+
303
+ if (pattern === apiPath) {
304
+ subsetHooks.push(...patternHooks.filter((h) => h.enabled));
305
+ continue;
306
+ }
307
+
308
+
309
+ for (const hook of patternHooks) {
310
+ if (!hook.enabled) continue;
311
+
312
+ if (!hook._compiled) {
313
+ hook._compiled = this.#compilePattern(hook.pattern);
314
+ }
315
+
316
+ if (hook._compiled(apiPath)) {
317
+ subsetHooks.push(hook);
318
+ }
319
+ }
320
+ }
321
+
322
+
323
+ subsetHooks.sort((a, b) => b.priority - a.priority);
324
+
325
+
326
+ hooks.push(...subsetHooks);
327
+ }
328
+
329
+ return hooks;
330
+ }
331
+
332
+
333
+ executeBeforeHooks(path, args, api, ctx) {
334
+ const hooks = this.getHooksForPath("before", path);
335
+
336
+ for (const hook of hooks) {
337
+ try {
338
+ const result = hook.handler({ path, args, api, ctx });
339
+
340
+
341
+ if (result && typeof result === "object" && typeof result.then === "function") {
342
+ throw new this.SlothletError("HOOK_BEFORE_RETURNED_PROMISE", { id: hook.id, path }, null, { validationError: true });
343
+ }
344
+
345
+
346
+ if (result !== undefined && !Array.isArray(result)) {
347
+ return { args, shortCircuit: true, value: result };
348
+ }
349
+
350
+
351
+ if (Array.isArray(result)) {
352
+ args = result;
353
+ }
354
+ } catch (error) {
355
+
356
+ const sourceInfo = {
357
+ type: "before",
358
+ subset: hook.subset,
359
+ hookTag: hook.id,
360
+ hookId: hook.id,
361
+ timestamp: Date.now(),
362
+ stack: error.stack
363
+ };
364
+ this.executeErrorHooks(path, error, sourceInfo, args, api, ctx);
365
+
366
+ if (!this.suppressErrors) {
367
+ throw error;
368
+ }
369
+
370
+ return { args, shortCircuit: true, value: undefined };
371
+ }
372
+ }
373
+
374
+ return { args, shortCircuit: false };
375
+ }
376
+
377
+
378
+ executeAfterHooks(path, result, args, api, ctx) {
379
+ const hooks = this.getHooksForPath("after", path);
380
+ const originalResult = result;
381
+ let currentResult = result;
382
+
383
+ for (const hook of hooks) {
384
+ try {
385
+ const hookContext = {
386
+ path,
387
+ args,
388
+ result: currentResult,
389
+ api,
390
+ ctx
391
+ };
392
+ const transformed = hook.handler(hookContext);
393
+
394
+
395
+ if (transformed !== undefined) {
396
+ currentResult = transformed;
397
+ }
398
+ } catch (error) {
399
+
400
+ const sourceInfo = {
401
+ type: "after",
402
+ subset: hook.subset,
403
+ hookTag: hook.id,
404
+ hookId: hook.id,
405
+ timestamp: Date.now(),
406
+ stack: error.stack
407
+ };
408
+ this.executeErrorHooks(path, error, sourceInfo, args, api, ctx);
409
+
410
+ if (!this.suppressErrors) {
411
+ throw error;
412
+ }
413
+ }
414
+ }
415
+
416
+
417
+ if (currentResult === originalResult) {
418
+ return { modified: false };
419
+ } else {
420
+ return { modified: true, result: currentResult };
421
+ }
422
+ }
423
+
424
+
425
+ executeAlwaysHooks(path, args, resultOrError, hasError = false, errors = [], api, ctx) {
426
+ const hooks = this.getHooksForPath("always", path);
427
+
428
+ for (const hook of hooks) {
429
+ try {
430
+ hook.handler({
431
+ path,
432
+ args,
433
+ result: hasError ? undefined : resultOrError,
434
+ hasError,
435
+ errors: errors,
436
+ api,
437
+ ctx
438
+ });
439
+ } catch (error) {
440
+
441
+ const sourceInfo = {
442
+ type: "always",
443
+ subset: hook.subset,
444
+ hookTag: hook.id,
445
+ hookId: hook.id,
446
+ timestamp: Date.now(),
447
+ stack: error.stack
448
+ };
449
+ this.executeErrorHooks(path, error, sourceInfo, args, api, ctx);
450
+ }
451
+ }
452
+ }
453
+
454
+
455
+ executeErrorHooks(path, error, source, args, api, ctx) {
456
+
457
+ if (error && typeof error === "object") {
458
+ error[ERROR_HOOK_PROCESSED] = true;
459
+ }
460
+
461
+ const hooks = this.getHooksForPath("error", path);
462
+
463
+ for (const hook of hooks) {
464
+ try {
465
+ hook.handler({
466
+ path,
467
+ args,
468
+ error,
469
+ errorType: error?.constructor?.name || "Error",
470
+ source,
471
+ timestamp: new Date(),
472
+ api,
473
+ ctx
474
+ });
475
+ } catch (hookError) {
476
+
477
+ this.slothlet.debug("hooks", `Error hook failed for ${path}:`, hookError);
478
+ }
479
+ }
480
+ }
481
+
482
+
483
+ #parseTypePattern(typePattern) {
484
+ if (typeof typePattern !== "string") {
485
+ throw new this.slothlet.SlothletError("INVALID_TYPE_PATTERN", {
486
+ typePattern,
487
+ expected: "string in format 'type:pattern'"
488
+ });
489
+ }
490
+
491
+ const colonIndex = typePattern.indexOf(":");
492
+ if (colonIndex === -1) {
493
+ throw new this.slothlet.SlothletError("INVALID_TYPE_PATTERN", {
494
+ typePattern,
495
+ expected: "string in format 'type:pattern' with at least one colon"
496
+ });
497
+ }
498
+
499
+ const type = typePattern.substring(0, colonIndex);
500
+ const pattern = typePattern.substring(colonIndex + 1);
501
+
502
+ if (!type || !pattern) {
503
+ throw new this.slothlet.SlothletError("INVALID_TYPE_PATTERN", {
504
+ typePattern,
505
+ expected: "non-empty type and pattern"
506
+ });
507
+ }
508
+
509
+ return { type, pattern };
510
+ }
511
+
512
+
513
+ #compilePattern(pattern) {
514
+
515
+ const isNegation = pattern.startsWith("!");
516
+ if (isNegation) {
517
+ pattern = pattern.slice(1);
518
+ const matcher = this.#compilePattern(pattern);
519
+ return (path) => !matcher(path);
520
+ }
521
+
522
+
523
+ const expanded = this.#expandBraces(pattern);
524
+ if (expanded.length > 1) {
525
+
526
+ const matchers = expanded.map((p) => this.#compilePattern(p));
527
+ return (path) => matchers.some((m) => m(path));
528
+ }
529
+
530
+
531
+ pattern = expanded[0];
532
+
533
+
534
+ let regexPattern = pattern
535
+ .replace(/[+^$()|[\]\\]/g, "\\$&")
536
+ .replace(/\*\*/g, "__DOUBLESTAR__")
537
+ .replace(/\*/g, "[^.]*")
538
+ .replace(/__DOUBLESTAR__/g, ".*")
539
+ .replace(/\?/g, ".");
540
+
541
+ regexPattern = `^${regexPattern}$`;
542
+ const regex = new RegExp(regexPattern);
543
+
544
+ return (path) => regex.test(path);
545
+ }
546
+
547
+
548
+ #expandBraces(pattern, depth = 0, maxDepth = 10) {
549
+
550
+ if (depth >= maxDepth) {
551
+ throw new this.SlothletError("HOOK_BRACE_EXPANSION_MAX_DEPTH", { maxDepth }, null, { validationError: true });
552
+ }
553
+
554
+
555
+ const braceStart = pattern.indexOf("{");
556
+ if (braceStart === -1) {
557
+ return [pattern];
558
+ }
559
+
560
+
561
+ let braceEnd = -1;
562
+ let depth_count = 1;
563
+ for (let i = braceStart + 1; i < pattern.length; i++) {
564
+ if (pattern[i] === "{") depth_count++;
565
+ if (pattern[i] === "}") {
566
+ depth_count--;
567
+ if (depth_count === 0) {
568
+ braceEnd = i;
569
+ break;
570
+ }
571
+ }
572
+ }
573
+
574
+ if (braceEnd === -1) {
575
+ return [pattern];
576
+ }
577
+
578
+
579
+ const prefix = pattern.slice(0, braceStart);
580
+ const braceContent = pattern.slice(braceStart + 1, braceEnd);
581
+ const suffix = pattern.slice(braceEnd + 1);
582
+
583
+
584
+ const alternatives = this.#splitBraceAlternatives(braceContent);
585
+
586
+
587
+ const expanded = [];
588
+ for (const alt of alternatives) {
589
+ const combined = prefix + alt + suffix;
590
+
591
+ const recursiveExpanded = this.#expandBraces(combined, depth + 1, maxDepth);
592
+ expanded.push(...recursiveExpanded);
593
+ }
594
+
595
+ return expanded;
596
+ }
597
+
598
+
599
+ #splitBraceAlternatives(content) {
600
+ const alternatives = [];
601
+ let current = "";
602
+ let depth = 0;
603
+
604
+ for (let i = 0; i < content.length; i++) {
605
+ const char = content[i];
606
+
607
+ if (char === "{") {
608
+ depth++;
609
+ current += char;
610
+ } else if (char === "}") {
611
+ depth--;
612
+ current += char;
613
+ } else if (char === "," && depth === 0) {
614
+ alternatives.push(current);
615
+ current = "";
616
+ } else {
617
+ current += char;
618
+ }
619
+ }
620
+
621
+ if (current) {
622
+ alternatives.push(current);
623
+ }
624
+
625
+ return alternatives;
626
+ }
627
+
628
+
629
+ getCompilePatternForDiagnostics() {
630
+ return this.#compilePattern.bind(this);
631
+ }
632
+
633
+
634
+ #generateId() {
635
+ return `hook-${++this.#idCounter}`;
636
+ }
637
+
638
+
639
+ #removeHook(hook) {
640
+ const typeIndex = this.#hooks[hook.type];
641
+ const subsetIndex = typeIndex[hook.subset];
642
+ const patternHooks = subsetIndex[hook.pattern];
643
+
644
+
645
+
646
+
647
+
648
+
649
+
650
+
651
+ if (!patternHooks) {
652
+ throw new this.slothlet.SlothletError("INTERNAL_HOOK_STATE_CORRUPT", {
653
+ hookId: hook.id,
654
+ type: hook.type,
655
+ subset: hook.subset,
656
+ pattern: hook.pattern,
657
+ detail: "patternHooks array missing from subsetIndex — #byId and #hooks are desynced"
658
+ });
659
+ }
660
+
661
+ const index = patternHooks.indexOf(hook);
662
+
663
+
664
+
665
+
666
+
667
+
668
+
669
+ if (index === -1) {
670
+ throw new this.slothlet.SlothletError("INTERNAL_HOOK_STATE_CORRUPT", {
671
+ hookId: hook.id,
672
+ type: hook.type,
673
+ subset: hook.subset,
674
+ pattern: hook.pattern,
675
+ detail: "hook object not found in patternHooks array — #byId and #hooks are desynced"
676
+ });
677
+ }
678
+
679
+ patternHooks.splice(index, 1);
680
+
681
+
682
+ if (patternHooks.length === 0) {
683
+ delete subsetIndex[hook.pattern];
684
+ }
685
+
686
+ this.#byId.delete(hook.id);
687
+ }
688
+
689
+
690
+ #setEnabledState(filter, enabled) {
691
+ let affected = 0;
692
+
693
+
694
+ if (filter.id) {
695
+ const hook = this.#byId.get(filter.id);
696
+ if (hook) {
697
+ hook.enabled = enabled;
698
+ affected = 1;
699
+ }
700
+ return affected;
701
+ }
702
+
703
+
704
+ const types = filter.type ? [filter.type] : Array.from(this.#validTypes);
705
+
706
+ for (const type of types) {
707
+ const typeIndex = this.#hooks[type];
708
+
709
+ for (const subset of ["before", "primary", "after"]) {
710
+ const subsetIndex = typeIndex[subset];
711
+
712
+
713
+ if (filter.pattern) {
714
+ const patternHooks = subsetIndex[filter.pattern] || [];
715
+ for (const hook of patternHooks) {
716
+ hook.enabled = enabled;
717
+ affected++;
718
+ }
719
+ } else {
720
+
721
+ for (const pattern in subsetIndex) {
722
+ const patternHooks = subsetIndex[pattern];
723
+ for (const hook of patternHooks) {
724
+ hook.enabled = enabled;
725
+ affected++;
726
+ }
727
+ }
728
+ }
729
+ }
730
+ }
731
+
732
+ return affected;
733
+ }
734
+
735
+
736
+ #serializeHook(hook) {
737
+ return {
738
+ id: hook.id,
739
+ type: hook.type,
740
+ pattern: hook.pattern,
741
+ priority: hook.priority,
742
+ subset: hook.subset,
743
+ enabled: hook.enabled
744
+
745
+ };
746
+ }
747
+
748
+
749
+ exportHooks() {
750
+ const registrations = [];
751
+ for (const hook of this.#byId.values()) {
752
+ registrations.push({
753
+ typePattern: `${hook.type}:${hook.pattern}`,
754
+ handler: hook.handler,
755
+ options: {
756
+ id: hook.id,
757
+ priority: hook.priority,
758
+ subset: hook.subset
759
+ },
760
+ enabled: hook.enabled
761
+ });
762
+ }
763
+ return registrations;
764
+ }
765
+
766
+
767
+ importHooks(registrations) {
768
+ if (!Array.isArray(registrations)) return;
769
+ for (const reg of registrations) {
770
+ this.on(reg.typePattern, reg.handler, reg.options);
771
+ if (!reg.enabled) {
772
+ this.disable({ id: reg.options.id });
773
+ }
774
+ }
775
+ }
776
+
777
+
778
+ async shutdown() {
779
+
780
+ this.#hooks = {
781
+ before: { before: {}, primary: {}, after: {} },
782
+ after: { before: {}, primary: {}, after: {} },
783
+ always: { before: {}, primary: {}, after: {} },
784
+ error: { before: {}, primary: {}, after: {} }
785
+ };
786
+ this.#byId.clear();
787
+ this.#idCounter = 0;
788
+ }
789
+ }