@cldmv/slothlet 2.9.0 → 2.11.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/AGENT-USAGE.md +41 -0
- package/README.md +95 -20
- package/dist/lib/engine/slothlet_child.mjs +1 -1
- package/dist/lib/engine/slothlet_engine.mjs +1 -1
- package/dist/lib/engine/slothlet_esm.mjs +1 -1
- package/dist/lib/engine/slothlet_helpers.mjs +1 -1
- package/dist/lib/engine/slothlet_worker.mjs +1 -1
- package/dist/lib/helpers/als-eventemitter.mjs +1 -1
- package/dist/lib/helpers/api_builder/add_api.mjs +321 -5
- package/dist/lib/helpers/api_builder/analysis.mjs +13 -3
- package/dist/lib/helpers/api_builder/construction.mjs +41 -3
- package/dist/lib/helpers/api_builder/decisions.mjs +16 -5
- package/dist/lib/helpers/api_builder/metadata.mjs +248 -0
- package/dist/lib/helpers/api_builder.mjs +1 -1
- package/dist/lib/helpers/auto-wrap.mjs +1 -1
- package/dist/lib/helpers/hooks.mjs +1 -1
- package/dist/lib/helpers/instance-manager.mjs +1 -1
- package/dist/lib/helpers/metadata-api.mjs +201 -0
- package/dist/lib/helpers/multidefault.mjs +12 -3
- package/dist/lib/helpers/resolve-from-caller.mjs +48 -11
- package/dist/lib/helpers/sanitize.mjs +1 -1
- package/dist/lib/helpers/utilities.mjs +1 -1
- package/dist/lib/modes/slothlet_eager.mjs +30 -2
- package/dist/lib/modes/slothlet_lazy.mjs +95 -5
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +5 -1
- package/dist/lib/runtime/runtime-livebindings.mjs +5 -1
- package/dist/lib/runtime/runtime.mjs +12 -1
- package/dist/slothlet.mjs +70 -6
- package/docs/API-RULES-CONDITIONS.md +511 -307
- package/docs/API-RULES.md +617 -589
- package/package.json +2 -2
- package/types/dist/lib/helpers/api_builder/add_api.d.mts +64 -22
- package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +1 -1
- package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +1 -1
- package/types/dist/lib/helpers/api_builder/construction.d.mts.map +1 -1
- package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +1 -1
- package/types/dist/lib/helpers/api_builder/metadata.d.mts +99 -0
- package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +1 -0
- package/types/dist/lib/helpers/metadata-api.d.mts +132 -0
- package/types/dist/lib/helpers/metadata-api.d.mts.map +1 -0
- package/types/dist/lib/helpers/multidefault.d.mts.map +1 -1
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +1 -1
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +2 -0
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +2 -0
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime.d.mts +1 -0
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +8 -0
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts.map +1 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright 2026 CLDMV/Shinrai
|
|
3
3
|
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
you may not use this file except in compliance with the License.
|
|
@@ -55,14 +55,19 @@ export async function analyzeModule(modulePath, options = {}) {
|
|
|
55
55
|
const moduleUrl = pathToFileURL(modulePath).href;
|
|
56
56
|
|
|
57
57
|
|
|
58
|
+
|
|
58
59
|
let importUrl = moduleUrl;
|
|
60
|
+
const separator = moduleUrl.includes("?") ? "&" : "?";
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
importUrl = `${moduleUrl}${separator}_t=${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
64
|
+
|
|
59
65
|
if (instance && instance.instanceId) {
|
|
60
66
|
const runtimeType = instance.config?.runtime || "async";
|
|
61
67
|
|
|
62
68
|
|
|
63
69
|
if (runtimeType === "live") {
|
|
64
|
-
|
|
65
|
-
importUrl = `${moduleUrl}${separator}slothlet_instance=${instance.instanceId}`;
|
|
70
|
+
importUrl = `${importUrl}&slothlet_instance=${instance.instanceId}`;
|
|
66
71
|
importUrl = `${importUrl}&slothlet_runtime=${runtimeType}`;
|
|
67
72
|
|
|
68
73
|
|
|
@@ -72,6 +77,11 @@ export async function analyzeModule(modulePath, options = {}) {
|
|
|
72
77
|
|
|
73
78
|
}
|
|
74
79
|
|
|
80
|
+
if (debug) {
|
|
81
|
+
console.log(`[DEBUG] analyzeModule: Importing ${path.basename(modulePath)}`);
|
|
82
|
+
console.log(`[DEBUG] URL: ${importUrl}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
75
85
|
const rawModule = await import(importUrl);
|
|
76
86
|
|
|
77
87
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright 2026 CLDMV/Shinrai
|
|
3
3
|
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
you may not use this file except in compliance with the License.
|
|
@@ -66,7 +66,8 @@ export async function buildCategoryStructure(categoryPath, options = {}) {
|
|
|
66
66
|
maxDepth,
|
|
67
67
|
mode,
|
|
68
68
|
subdirHandler,
|
|
69
|
-
instance
|
|
69
|
+
instance,
|
|
70
|
+
existingApi: options.existingApi
|
|
70
71
|
});
|
|
71
72
|
|
|
72
73
|
|
|
@@ -320,6 +321,34 @@ export async function buildCategoryStructure(categoryPath, options = {}) {
|
|
|
320
321
|
}
|
|
321
322
|
|
|
322
323
|
|
|
324
|
+
|
|
325
|
+
let defaultFunctions = [];
|
|
326
|
+
for (const moduleDecision of processedModules) {
|
|
327
|
+
const { mod, type } = moduleDecision;
|
|
328
|
+
if (type === "function" && typeof mod === "function") {
|
|
329
|
+
defaultFunctions.push({ mod, moduleDecision });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
if (defaultFunctions.length === 1 && Object.keys(categoryModules).length === 1) {
|
|
337
|
+
const categoryDefaultFunction = defaultFunctions[0].mod;
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
if (categoryDefaultFunction.name !== categoryName) {
|
|
341
|
+
try {
|
|
342
|
+
Object.defineProperty(categoryDefaultFunction, "name", { value: categoryName, configurable: true });
|
|
343
|
+
} catch {
|
|
344
|
+
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return categoryDefaultFunction;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
323
352
|
const keys = Object.keys(categoryModules);
|
|
324
353
|
if (keys.length === 1) {
|
|
325
354
|
const singleKey = keys[0];
|
|
@@ -416,7 +445,16 @@ export async function buildRootAPI(dir, options = {}) {
|
|
|
416
445
|
|
|
417
446
|
if (lazy) {
|
|
418
447
|
|
|
419
|
-
api[instance._toapiPathKey(entry.name)] = await
|
|
448
|
+
api[instance._toapiPathKey(entry.name)] = await buildCategoryStructure(categoryPath, {
|
|
449
|
+
currentDepth: 1,
|
|
450
|
+
maxDepth,
|
|
451
|
+
mode: "lazy",
|
|
452
|
+
subdirHandler: (ctx) => {
|
|
453
|
+
|
|
454
|
+
return instance._loadCategory(ctx.subDirPath, ctx.currentDepth, ctx.maxDepth);
|
|
455
|
+
},
|
|
456
|
+
instance
|
|
457
|
+
});
|
|
420
458
|
} else {
|
|
421
459
|
|
|
422
460
|
api[instance._toapiPathKey(entry.name)] = await buildCategoryStructure(categoryPath, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright 2026 CLDMV/Shinrai
|
|
3
3
|
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
you may not use this file except in compliance with the License.
|
|
@@ -321,10 +321,21 @@ export function processModuleForAPI(options) {
|
|
|
321
321
|
|
|
322
322
|
|
|
323
323
|
for (const [key, value] of Object.entries(apiAssignments)) {
|
|
324
|
-
if (debug
|
|
325
|
-
console.log(`[DEBUG] ${mode}:
|
|
324
|
+
if (debug) {
|
|
325
|
+
console.log(`[DEBUG] ${mode}: About to assign key '${key}' - current api[${key}]:`, api[key] ? Object.keys(api[key]) : "undefined");
|
|
326
|
+
console.log(`[DEBUG] ${mode}: New value for key '${key}':`, typeof value === "object" && value ? Object.keys(value) : typeof value);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
if (api[key] && typeof api[key] === "object" && typeof value === "object" && !Array.isArray(api[key]) && !Array.isArray(value)) {
|
|
331
|
+
if (debug) {
|
|
332
|
+
console.log(`[DEBUG] ${mode}: Merging objects for key '${key}' - existing:`, Object.keys(api[key]), "new:", Object.keys(value));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
Object.assign(api[key], value);
|
|
336
|
+
} else {
|
|
337
|
+
api[key] = value;
|
|
326
338
|
}
|
|
327
|
-
api[key] = value;
|
|
328
339
|
}
|
|
329
340
|
|
|
330
341
|
return {
|
|
@@ -342,7 +353,7 @@ export function processModuleForAPI(options) {
|
|
|
342
353
|
|
|
343
354
|
|
|
344
355
|
export async function buildCategoryDecisions(categoryPath, options = {}) {
|
|
345
|
-
const { currentDepth = 0, maxDepth = Infinity, mode = "eager", subdirHandler } = options;
|
|
356
|
+
const { currentDepth = 0, maxDepth = Infinity, mode = "eager", subdirHandler, existingApi: _ } = options;
|
|
346
357
|
const { instance } = options;
|
|
347
358
|
|
|
348
359
|
if (!instance || typeof instance._toapiPathKey !== "function") {
|
|
@@ -0,0 +1,248 @@
|
|
|
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
|
+
|
|
22
|
+
export function createImmutableMetadata(initial = {}) {
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
function makeImmutable(obj, visited = new WeakSet()) {
|
|
27
|
+
if (obj === null || typeof obj !== "object") return obj;
|
|
28
|
+
if (visited.has(obj)) return obj;
|
|
29
|
+
visited.add(obj);
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(obj)) {
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < obj.length; i++) {
|
|
35
|
+
obj[i] = makeImmutable(obj[i], visited);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
return new Proxy(obj, {
|
|
41
|
+
set() {
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
return false;
|
|
45
|
+
},
|
|
46
|
+
deleteProperty() {
|
|
47
|
+
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
55
|
+
const immutableValue = makeImmutable(value, visited);
|
|
56
|
+
Object.defineProperty(obj, key, {
|
|
57
|
+
value: immutableValue,
|
|
58
|
+
writable: false,
|
|
59
|
+
enumerable: true,
|
|
60
|
+
configurable: true
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
return new Proxy(obj, {
|
|
66
|
+
set(target, prop, value) {
|
|
67
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if (descriptor && !descriptor.writable) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
if (!descriptor) {
|
|
76
|
+
const immutableValue = makeImmutable(value);
|
|
77
|
+
Object.defineProperty(target, prop, {
|
|
78
|
+
value: immutableValue,
|
|
79
|
+
writable: false,
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true
|
|
82
|
+
});
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return Reflect.set(target, prop, value);
|
|
87
|
+
},
|
|
88
|
+
deleteProperty() {
|
|
89
|
+
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
const base = {};
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
for (const [key, value] of Object.entries(initial)) {
|
|
101
|
+
const immutableValue = makeImmutable(value);
|
|
102
|
+
Object.defineProperty(base, key, {
|
|
103
|
+
value: immutableValue,
|
|
104
|
+
writable: false,
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
return new Proxy(base, {
|
|
112
|
+
set(target, prop, value) {
|
|
113
|
+
|
|
114
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, prop);
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
if (descriptor && !descriptor.writable) {
|
|
118
|
+
if (process.env.SLOTHLET_DEBUG) {
|
|
119
|
+
console.warn(`[slothlet] Cannot modify existing metadata property: "${String(prop)}"`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if (!descriptor) {
|
|
128
|
+
Object.defineProperty(target, prop, {
|
|
129
|
+
value,
|
|
130
|
+
writable: false,
|
|
131
|
+
enumerable: true,
|
|
132
|
+
configurable: true
|
|
133
|
+
});
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
return Reflect.set(target, prop, value);
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
deleteProperty(target, prop) {
|
|
143
|
+
|
|
144
|
+
if (process.env.SLOTHLET_DEBUG) {
|
|
145
|
+
console.warn(`[slothlet] Cannot delete metadata property: "${String(prop)}"`);
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
export function cleanMetadata(obj, visited = new WeakSet()) {
|
|
154
|
+
|
|
155
|
+
if (!obj || (typeof obj !== "object" && typeof obj !== "function")) return;
|
|
156
|
+
if (visited.has(obj)) return;
|
|
157
|
+
visited.add(obj);
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
if (typeof obj === "function") {
|
|
161
|
+
try {
|
|
162
|
+
if (obj.__metadata) {
|
|
163
|
+
delete obj.__metadata;
|
|
164
|
+
}
|
|
165
|
+
if (obj.__sourceFolder) {
|
|
166
|
+
delete obj.__sourceFolder;
|
|
167
|
+
}
|
|
168
|
+
} catch (_) {
|
|
169
|
+
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
const keys = Object.keys(obj);
|
|
176
|
+
for (const key of keys) {
|
|
177
|
+
if (
|
|
178
|
+
key.startsWith("_") ||
|
|
179
|
+
["hooks", "shutdown", "addApi", "describe", "run", "__proto__", "constructor", "prototype"].includes(key)
|
|
180
|
+
) {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
cleanMetadata(obj[key], visited);
|
|
184
|
+
}
|
|
185
|
+
} catch {
|
|
186
|
+
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
export function tagLoadedFunctions(obj, metadata, baseDir, visited = new WeakSet()) {
|
|
192
|
+
|
|
193
|
+
if (!obj || (typeof obj !== "object" && typeof obj !== "function")) return;
|
|
194
|
+
if (visited.has(obj)) return;
|
|
195
|
+
visited.add(obj);
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
if (typeof obj === "function") {
|
|
199
|
+
try {
|
|
200
|
+
|
|
201
|
+
const immutableMeta = createImmutableMetadata(metadata);
|
|
202
|
+
|
|
203
|
+
Object.defineProperty(obj, "__metadata", {
|
|
204
|
+
value: immutableMeta,
|
|
205
|
+
writable: false,
|
|
206
|
+
enumerable: false,
|
|
207
|
+
configurable: true
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
if (metadata.sourceFolder) {
|
|
212
|
+
Object.defineProperty(obj, "__sourceFolder", {
|
|
213
|
+
value: metadata.sourceFolder,
|
|
214
|
+
writable: false,
|
|
215
|
+
enumerable: false,
|
|
216
|
+
configurable: true
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
} catch (error) {
|
|
224
|
+
|
|
225
|
+
if (metadata.debug) {
|
|
226
|
+
console.warn(`[slothlet] Could not tag function with metadata:`, error.message);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
try {
|
|
233
|
+
const keys = Object.keys(obj);
|
|
234
|
+
for (const key of keys) {
|
|
235
|
+
|
|
236
|
+
if (
|
|
237
|
+
key.startsWith("_") ||
|
|
238
|
+
["hooks", "shutdown", "addApi", "describe", "run", "__proto__", "constructor", "prototype"].includes(key)
|
|
239
|
+
) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
tagLoadedFunctions(obj[key], metadata, baseDir, visited);
|
|
244
|
+
}
|
|
245
|
+
} catch {
|
|
246
|
+
|
|
247
|
+
}
|
|
248
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
Copyright
|
|
2
|
+
Copyright 2026 CLDMV/Shinrai
|
|
3
3
|
|
|
4
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
you may not use this file except in compliance with the License.
|
|
@@ -39,10 +39,19 @@ async function multidefault_analyzeModules(moduleFiles, baseDir, options = {}) {
|
|
|
39
39
|
const moduleFilePath = path.resolve(baseDir, file.name);
|
|
40
40
|
|
|
41
41
|
|
|
42
|
+
|
|
42
43
|
let importUrl = `file://${moduleFilePath.replace(/\\/g, "/")}`;
|
|
44
|
+
const separator = importUrl.includes("?") ? "&" : "?";
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
importUrl = `${importUrl}${separator}_t=${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
48
|
+
|
|
49
|
+
|
|
43
50
|
if (instance && instance.instanceId) {
|
|
44
|
-
const
|
|
45
|
-
|
|
51
|
+
const runtimeType = instance.config?.runtime || "async";
|
|
52
|
+
if (runtimeType === "live") {
|
|
53
|
+
importUrl = `${importUrl}&slothlet_instance=${instance.instanceId}`;
|
|
54
|
+
}
|
|
46
55
|
}
|
|
47
56
|
|
|
48
57
|
|