@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
@@ -1,389 +0,0 @@
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
- const MAX_BRACE_NESTING = 10;
22
-
23
-
24
- export class HookManager {
25
-
26
- constructor(enabled = true, defaultPattern = "**", options = {}) {
27
- this.enabled = enabled;
28
- this.defaultPattern = defaultPattern;
29
- this.suppressErrors = options.suppressErrors || false;
30
- this.hooks = new Map();
31
- this.registrationOrder = 0;
32
- this.reportedErrors = new WeakSet();
33
- }
34
-
35
-
36
- on(name, type, handler, options = {}) {
37
- const priority = options.priority ?? 100;
38
- const pattern = options.pattern || this.defaultPattern;
39
- const compiledPattern = this._compilePattern(pattern);
40
- const order = this.registrationOrder++;
41
-
42
- this.hooks.set(name, {
43
- tag: name,
44
- type,
45
- handler,
46
- priority,
47
- pattern,
48
- compiledPattern,
49
- order
50
- });
51
-
52
- return name;
53
- }
54
-
55
-
56
- cleanup() {
57
- this.hooks.clear();
58
- this.reportedErrors = new WeakSet();
59
- this.registrationOrder = 0;
60
- this.enabled = false;
61
- }
62
-
63
-
64
- off(nameOrPattern) {
65
-
66
- if (this.hooks.has(nameOrPattern)) {
67
- return this.hooks.delete(nameOrPattern);
68
- }
69
-
70
-
71
- const compiled = this._compilePattern(nameOrPattern);
72
- let removed = false;
73
- for (const key of [...this.hooks.keys()]) {
74
- if (this._matchPattern(compiled, key)) {
75
- this.hooks.delete(key);
76
- removed = true;
77
- }
78
- }
79
- return removed;
80
- }
81
-
82
-
83
- clear(type) {
84
- if (type === undefined) {
85
- this.hooks.clear();
86
- this.registrationOrder = 0;
87
- return;
88
- }
89
-
90
-
91
- for (const [name, hook] of this.hooks) {
92
- if (hook.type === type) {
93
- this.hooks.delete(name);
94
- }
95
- }
96
- }
97
-
98
-
99
- list(type) {
100
- const result = [];
101
- for (const [name, hook] of this.hooks) {
102
- if (type === undefined || hook.type === type) {
103
- result.push({
104
- name,
105
- type: hook.type,
106
- priority: hook.priority,
107
- pattern: hook.pattern,
108
- order: hook.order
109
- });
110
- }
111
- }
112
- return result;
113
- }
114
-
115
-
116
- enable(pattern) {
117
- this.enabled = true;
118
- if (pattern !== undefined) {
119
- this.defaultPattern = pattern;
120
- }
121
- }
122
-
123
-
124
- disable() {
125
- this.enabled = false;
126
- }
127
-
128
-
129
- executeBeforeHooks(path, args) {
130
- const hooks = this._getMatchingHooks("before", path);
131
- let currentArgs = args;
132
-
133
- for (const hook of hooks) {
134
- try {
135
- const result = hook.handler({ path, args: currentArgs });
136
-
137
-
138
- if (result === undefined) {
139
- continue;
140
- }
141
-
142
-
143
- if (Array.isArray(result)) {
144
- currentArgs = result;
145
- continue;
146
- }
147
-
148
-
149
- return { cancelled: true, value: result, args: currentArgs };
150
- } catch (error) {
151
-
152
- this.reportedErrors.add(error);
153
- this.executeErrorHooks(path, error, {
154
- type: "before",
155
- hookId: hook.id,
156
- hookTag: hook.tag
157
- });
158
- throw error;
159
- }
160
- }
161
-
162
- return { cancelled: false, args: currentArgs };
163
- }
164
-
165
-
166
- executeAfterHooks(path, initialResult) {
167
- const hooks = this._getMatchingHooks("after", path);
168
- let currentResult = initialResult;
169
-
170
- for (const hook of hooks) {
171
- try {
172
- const transformed = hook.handler({ path, result: currentResult });
173
-
174
- if (transformed !== undefined) {
175
- currentResult = transformed;
176
- }
177
- } catch (error) {
178
-
179
- this.reportedErrors.add(error);
180
- this.executeErrorHooks(path, error, {
181
- type: "after",
182
- hookId: hook.id,
183
- hookTag: hook.tag
184
- });
185
- throw error;
186
- }
187
- }
188
-
189
- return currentResult;
190
- }
191
-
192
-
193
- executeAlwaysHooks(path, result, errors = []) {
194
- const hooks = this._getMatchingHooks("always", path);
195
-
196
- for (const hook of hooks) {
197
- try {
198
- hook.handler({
199
- path,
200
- result,
201
- hasError: errors.length > 0,
202
- errors
203
- });
204
- } catch (error) {
205
-
206
- this.executeErrorHooks(path, error, {
207
- type: "always",
208
- hookId: hook.id,
209
- hookTag: hook.tag
210
- });
211
-
212
- }
213
- }
214
- }
215
-
216
-
217
- executeErrorHooks(path, error, source = { type: "unknown" }) {
218
- const hooks = this._getMatchingHooks("error", path);
219
-
220
-
221
- const errorContext = {
222
- path,
223
- error,
224
- errorType: error.constructor ? error.constructor.name : "Error",
225
- source: {
226
- type: source.type || "unknown",
227
- hookId: source.hookId,
228
- hookTag: source.hookTag,
229
- timestamp: Date.now(),
230
- stack: error.stack
231
- }
232
- };
233
-
234
- for (const hook of hooks) {
235
- try {
236
- hook.handler(errorContext);
237
- } catch (hookError) {
238
-
239
- console.error(`Error in error hook for ${path}:`, hookError);
240
- }
241
- }
242
- }
243
-
244
-
245
- _getMatchingHooks(type, path) {
246
- const matching = [];
247
-
248
- for (const [hookId, hook] of this.hooks.entries()) {
249
- if (hook.type !== type) continue;
250
- if (!this._matchPattern(hook.compiledPattern, path)) continue;
251
- matching.push({ ...hook, id: hookId });
252
- }
253
-
254
-
255
- matching.sort((a, b) => {
256
- if (a.priority !== b.priority) {
257
- return b.priority - a.priority;
258
- }
259
- return a.order - b.order;
260
- });
261
-
262
- return matching;
263
- }
264
-
265
-
266
- _compilePattern(pattern) {
267
-
268
- if (pattern.startsWith("!")) {
269
- return {
270
- negation: true,
271
- regex: this._compilePattern(pattern.slice(1))
272
- };
273
- }
274
-
275
-
276
- const expanded = this._expandBraces(pattern);
277
- if (expanded.length > 1) {
278
-
279
- const regexes = expanded.map((p) => this._patternToRegex(p));
280
- return new RegExp(`^(?:${regexes.join("|")})$`);
281
- }
282
-
283
-
284
- return new RegExp(`^${this._patternToRegex(expanded[0])}$`);
285
- }
286
-
287
-
288
- _expandBraces(pattern, depth = 0) {
289
- if (depth > MAX_BRACE_NESTING) {
290
- throw new Error(`Brace expansion exceeds maximum nesting depth of ${MAX_BRACE_NESTING}`);
291
- }
292
-
293
- const braceStart = pattern.indexOf("{");
294
- if (braceStart === -1) {
295
- return [pattern];
296
- }
297
-
298
-
299
- let braceEnd = -1;
300
- let nestLevel = 0;
301
- for (let i = braceStart; i < pattern.length; i++) {
302
- if (pattern[i] === "{") nestLevel++;
303
- if (pattern[i] === "}") {
304
- nestLevel--;
305
- if (nestLevel === 0) {
306
- braceEnd = i;
307
- break;
308
- }
309
- }
310
- }
311
-
312
- if (braceEnd === -1) {
313
-
314
- return [pattern];
315
- }
316
-
317
- const before = pattern.slice(0, braceStart);
318
- const inside = pattern.slice(braceStart + 1, braceEnd);
319
- const after = pattern.slice(braceEnd + 1);
320
-
321
-
322
- const alternatives = this._splitAlternatives(inside);
323
-
324
-
325
- const results = [];
326
- for (const alt of alternatives) {
327
- const expanded = this._expandBraces(before + alt + after, depth + 1);
328
- results.push(...expanded);
329
- }
330
-
331
- return results;
332
- }
333
-
334
-
335
- _splitAlternatives(str) {
336
- const alternatives = [];
337
- let current = "";
338
- let nestLevel = 0;
339
-
340
- for (let i = 0; i < str.length; i++) {
341
- const char = str[i];
342
- if (char === "{") nestLevel++;
343
- if (char === "}") nestLevel--;
344
-
345
- if (char === "," && nestLevel === 0) {
346
- alternatives.push(current);
347
- current = "";
348
- } else {
349
- current += char;
350
- }
351
- }
352
-
353
- if (current) {
354
- alternatives.push(current);
355
- }
356
-
357
- return alternatives;
358
- }
359
-
360
-
361
- _patternToRegex(pattern) {
362
-
363
- let regex = pattern.replace(/[+?^${}()|[\]\\]/g, "\\$&");
364
-
365
-
366
- regex = regex.replace(/\./g, "\\.");
367
-
368
-
369
- regex = regex.replace(/\*\*/g, "___DOUBLESTAR___");
370
-
371
-
372
- regex = regex.replace(/\*/g, "([^\\.]+)");
373
-
374
-
375
- regex = regex.replace(/___DOUBLESTAR___/g, ".*?");
376
-
377
- return regex;
378
- }
379
-
380
-
381
- _matchPattern(compiledPattern, path) {
382
- if (compiledPattern.negation) {
383
-
384
- return !this._matchPattern(compiledPattern.regex, path);
385
- }
386
-
387
- return compiledPattern.test(path);
388
- }
389
- }
@@ -1,111 +0,0 @@
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
- const instanceRegistry = new Map();
21
-
22
-
23
- let currentActiveInstanceId = null;
24
-
25
-
26
- export function getInstanceData(instanceId) {
27
- return instanceRegistry.get(instanceId) || null;
28
- }
29
-
30
-
31
- export function updateInstanceData(instanceId, key, value) {
32
-
33
-
34
-
35
-
36
-
37
-
38
- let instanceData = instanceRegistry.get(instanceId);
39
- if (!instanceData) {
40
- instanceData = {
41
- self: null,
42
- context: {},
43
- reference: {}
44
- };
45
- instanceRegistry.set(instanceId, instanceData);
46
- }
47
- instanceData[key] = value;
48
-
49
-
50
- }
51
-
52
-
53
- export async function cleanupInstance(instanceId) {
54
-
55
- instanceRegistry.delete(instanceId);
56
-
57
- if (currentActiveInstanceId === instanceId) {
58
- currentActiveInstanceId = null;
59
- }
60
- }
61
-
62
-
63
- export function setActiveInstance(instanceId) {
64
- currentActiveInstanceId = instanceId;
65
-
66
- }
67
-
68
-
69
- export function getCurrentActiveInstanceId() {
70
- return currentActiveInstanceId;
71
- }
72
-
73
-
74
- export function detectCurrentInstanceId() {
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
- if (currentActiveInstanceId && instanceRegistry.has(currentActiveInstanceId)) {
84
- return currentActiveInstanceId;
85
- }
86
-
87
-
88
- const stack = new Error().stack;
89
- if (stack) {
90
-
91
- const matches = stack.match(/slothlet_instance=([^&\s):]+)/g);
92
-
93
- if (matches && matches.length > 0) {
94
-
95
- const instanceParam = matches[0];
96
- const instanceId = instanceParam.replace(/slothlet_instance=([^&\s):]+).*/, "$1");
97
-
98
- if (instanceRegistry.has(instanceId)) {
99
- return instanceId;
100
- }
101
- }
102
- }
103
-
104
-
105
- return null;
106
- }
107
-
108
-
109
- export function getAllInstanceIds() {
110
- return Array.from(instanceRegistry.keys());
111
- }
@@ -1,201 +0,0 @@
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 { getStack, toFsPath } from "@cldmv/slothlet/helpers/resolve-from-caller";
22
-
23
-
24
- let runtimeModule = null;
25
- let runtimeImportPromise = null;
26
-
27
-
28
- async function ensureRuntime() {
29
- if (runtimeModule) {
30
- return runtimeModule;
31
- }
32
-
33
- if (!runtimeImportPromise) {
34
- runtimeImportPromise = import("@cldmv/slothlet/runtime")
35
- .then((module) => {
36
- runtimeModule = module;
37
- return module;
38
- })
39
- .catch((err) => {
40
- console.error("[slothlet] Failed to import runtime for metadata API:", err.message);
41
- runtimeModule = {};
42
- return {};
43
- });
44
- }
45
-
46
- return runtimeImportPromise;
47
- }
48
-
49
-
50
- function getApiRoot() {
51
-
52
- return runtimeModule?.self || null;
53
- }
54
-
55
-
56
- function findFunctionByStack(apiRoot, targetFile, targetLine, visited = new WeakSet()) {
57
- if (!apiRoot || visited.has(apiRoot)) return null;
58
- visited.add(apiRoot);
59
-
60
-
61
- if (typeof apiRoot === "function" && apiRoot.__sourceFile && apiRoot.__sourceLine) {
62
- if (apiRoot.__sourceFile === targetFile && apiRoot.__sourceLine === targetLine) {
63
- return apiRoot;
64
- }
65
- }
66
-
67
-
68
- if (typeof apiRoot === "object" || typeof apiRoot === "function") {
69
- const keys = Object.keys(apiRoot);
70
- for (const key of keys) {
71
-
72
- if (key.startsWith("_") || ["hooks", "shutdown", "addApi", "describe", "run"].includes(key)) {
73
- continue;
74
- }
75
-
76
- const result = findFunctionByStack(apiRoot[key], targetFile, targetLine, visited);
77
- if (result) return result;
78
- }
79
- }
80
-
81
- return null;
82
- }
83
-
84
-
85
- function findFunctionByPath(apiRoot, path) {
86
- if (!path || typeof path !== "string") return null;
87
-
88
- const parts = path.split(".");
89
- let current = apiRoot;
90
-
91
- for (const part of parts) {
92
- if (!current || (typeof current !== "object" && typeof current !== "function")) {
93
- if (process.env.SLOTHLET_DEBUG) {
94
- console.log("[findFunctionByPath] Failed: current is", typeof current);
95
- }
96
- return null;
97
- }
98
- current = current[part];
99
- }
100
-
101
- const result = typeof current === "function" ? current : null;
102
- if (process.env.SLOTHLET_DEBUG) {
103
- console.log("[findFunctionByPath] Result:", result ? "function found" : "null");
104
- }
105
- return result;
106
- }
107
-
108
-
109
- function parseCallSite(cs) {
110
- if (!cs) return null;
111
-
112
- const fileName = cs.getFileName?.();
113
- if (!fileName) return null;
114
-
115
-
116
- const filePath = toFsPath(fileName);
117
- if (!filePath) return null;
118
-
119
-
120
- if (filePath.startsWith?.("node:internal")) return null;
121
-
122
- const lineNum = cs.getLineNumber?.();
123
- if (typeof lineNum !== "number") return null;
124
-
125
- return { file: filePath, line: lineNum };
126
- }
127
-
128
-
129
- export const metadataAPI = {
130
-
131
- async caller() {
132
-
133
- await ensureRuntime();
134
-
135
-
136
- const apiRoot = getApiRoot();
137
- if (!apiRoot || typeof apiRoot !== "object") return null;
138
-
139
-
140
- const stack = getStack(metadataAPI.caller);
141
-
142
-
143
-
144
-
145
- if (stack.length < 1) return null;
146
-
147
- const parsed = parseCallSite(stack[0]);
148
- if (!parsed) return null;
149
-
150
-
151
- const func = findFunctionByStack(apiRoot, parsed.file, parsed.line);
152
- if (!func) return null;
153
-
154
-
155
- return func.__metadata || null;
156
- },
157
-
158
-
159
- async self() {
160
-
161
- await ensureRuntime();
162
-
163
-
164
- const apiRoot = getApiRoot();
165
- if (!apiRoot || typeof apiRoot !== "object") return null;
166
-
167
-
168
- const stack = getStack(metadataAPI.self);
169
-
170
-
171
- if (stack.length < 1) return null;
172
-
173
- const parsed = parseCallSite(stack[0]);
174
- if (!parsed) return null;
175
-
176
-
177
- const func = findFunctionByStack(apiRoot, parsed.file, parsed.line);
178
- if (!func) return null;
179
-
180
-
181
- return func.__metadata || null;
182
- },
183
-
184
-
185
- async get(path, apiRoot) {
186
-
187
- await ensureRuntime();
188
-
189
-
190
- const root = apiRoot || getApiRoot();
191
- if (!root || (typeof root !== "object" && typeof root !== "function")) {
192
- return null;
193
- }
194
-
195
- const func = findFunctionByPath(root, path);
196
- if (!func) return null;
197
-
198
-
199
- return func.__metadata || null;
200
- }
201
- };