@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.
- package/AGENT-USAGE.md +355 -325
- package/README.md +554 -238
- package/dist/lib/builders/api-assignment.mjs +605 -0
- package/dist/lib/builders/api_builder.mjs +1073 -0
- package/dist/lib/builders/builder.mjs +94 -0
- package/dist/lib/builders/modes-processor.mjs +1816 -0
- package/dist/lib/errors.mjs +227 -0
- package/dist/lib/factories/component-base.mjs +96 -0
- package/dist/lib/factories/context.mjs +38 -0
- package/dist/lib/handlers/api-cache-manager.mjs +216 -0
- package/dist/lib/handlers/api-manager.mjs +2364 -0
- package/dist/lib/handlers/context-async.mjs +184 -0
- package/dist/lib/handlers/context-live.mjs +184 -0
- package/dist/lib/handlers/hook-manager.mjs +789 -0
- package/dist/lib/handlers/lifecycle-token.mjs +44 -0
- package/dist/lib/handlers/lifecycle.mjs +131 -0
- package/dist/lib/handlers/materialize-manager.mjs +64 -0
- package/dist/lib/handlers/metadata.mjs +500 -0
- package/dist/lib/handlers/ownership.mjs +338 -0
- package/dist/lib/handlers/unified-wrapper.mjs +3031 -0
- package/dist/lib/helpers/class-instance-wrapper.mjs +125 -0
- package/dist/lib/helpers/config.mjs +343 -0
- package/dist/lib/helpers/eventemitter-context.mjs +365 -0
- package/dist/lib/helpers/hint-detector.mjs +63 -0
- package/dist/lib/helpers/modes-utils.mjs +53 -0
- package/dist/lib/helpers/resolve-from-caller.mjs +123 -117
- package/dist/lib/helpers/sanitize.mjs +247 -168
- package/dist/lib/helpers/utilities.mjs +46 -81
- package/dist/lib/i18n/languages/de-de.json +377 -0
- package/dist/lib/i18n/languages/en-gb.json +377 -0
- package/dist/lib/i18n/languages/en-us.json +377 -0
- package/dist/lib/i18n/languages/es-mx.json +377 -0
- package/dist/lib/i18n/languages/fr-fr.json +377 -0
- package/dist/lib/i18n/languages/hi-in.json +377 -0
- package/dist/lib/i18n/languages/ja-jp.json +377 -0
- package/dist/lib/i18n/languages/ko-kr.json +377 -0
- package/dist/lib/i18n/languages/pt-br.json +377 -0
- package/dist/lib/i18n/languages/ru-ru.json +377 -0
- package/dist/lib/i18n/languages/zh-cn.json +377 -0
- package/dist/lib/i18n/translations.mjs +140 -0
- package/dist/lib/modes/eager.mjs +75 -0
- package/dist/lib/modes/lazy.mjs +97 -0
- package/dist/lib/processors/flatten.mjs +453 -0
- package/dist/lib/processors/loader.mjs +355 -0
- package/dist/lib/processors/type-generator.mjs +291 -0
- package/dist/lib/processors/typescript.mjs +188 -0
- package/dist/lib/runtime/runtime-asynclocalstorage.mjs +80 -522
- package/dist/lib/runtime/runtime-livebindings.mjs +45 -390
- package/dist/lib/runtime/runtime.mjs +39 -159
- package/dist/slothlet.mjs +525 -744
- package/docs/API-RULES.md +338 -486
- package/index.cjs +4 -4
- package/index.mjs +82 -45
- package/package.json +143 -30
- package/types/dist/lib/builders/api-assignment.d.mts +97 -0
- package/types/dist/lib/builders/api-assignment.d.mts.map +1 -0
- package/types/dist/lib/builders/api_builder.d.mts +96 -0
- package/types/dist/lib/builders/api_builder.d.mts.map +1 -0
- package/types/dist/lib/builders/builder.d.mts +60 -0
- package/types/dist/lib/builders/builder.d.mts.map +1 -0
- package/types/dist/lib/builders/modes-processor.d.mts +32 -0
- package/types/dist/lib/builders/modes-processor.d.mts.map +1 -0
- package/types/dist/lib/errors.d.mts +118 -0
- package/types/dist/lib/errors.d.mts.map +1 -0
- package/types/dist/lib/factories/component-base.d.mts +182 -0
- package/types/dist/lib/factories/component-base.d.mts.map +1 -0
- package/types/dist/lib/factories/context.d.mts +26 -0
- package/types/dist/lib/factories/context.d.mts.map +1 -0
- package/types/dist/lib/handlers/api-cache-manager.d.mts +208 -0
- package/types/dist/lib/handlers/api-cache-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/api-manager.d.mts +392 -0
- package/types/dist/lib/handlers/api-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/context-async.d.mts +66 -0
- package/types/dist/lib/handlers/context-async.d.mts.map +1 -0
- package/types/dist/lib/handlers/context-live.d.mts +65 -0
- package/types/dist/lib/handlers/context-live.d.mts.map +1 -0
- package/types/dist/lib/handlers/hook-manager.d.mts +199 -0
- package/types/dist/lib/handlers/hook-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/lifecycle-token.d.mts +49 -0
- package/types/dist/lib/handlers/lifecycle-token.d.mts.map +1 -0
- package/types/dist/lib/handlers/lifecycle.d.mts +90 -0
- package/types/dist/lib/handlers/lifecycle.d.mts.map +1 -0
- package/types/dist/lib/handlers/materialize-manager.d.mts +75 -0
- package/types/dist/lib/handlers/materialize-manager.d.mts.map +1 -0
- package/types/dist/lib/handlers/metadata.d.mts +215 -0
- package/types/dist/lib/handlers/metadata.d.mts.map +1 -0
- package/types/dist/lib/handlers/ownership.d.mts +170 -0
- package/types/dist/lib/handlers/ownership.d.mts.map +1 -0
- package/types/dist/lib/handlers/unified-wrapper.d.mts +250 -0
- package/types/dist/lib/handlers/unified-wrapper.d.mts.map +1 -0
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts +54 -0
- package/types/dist/lib/helpers/class-instance-wrapper.d.mts.map +1 -0
- package/types/dist/lib/helpers/config.d.mts +96 -0
- package/types/dist/lib/helpers/config.d.mts.map +1 -0
- package/types/dist/lib/helpers/eventemitter-context.d.mts +31 -0
- package/types/dist/lib/helpers/eventemitter-context.d.mts.map +1 -0
- package/types/dist/lib/helpers/hint-detector.d.mts +20 -0
- package/types/dist/lib/helpers/hint-detector.d.mts.map +1 -0
- package/types/dist/lib/helpers/modes-utils.d.mts +35 -0
- package/types/dist/lib/helpers/modes-utils.d.mts.map +1 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +29 -145
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -1
- package/types/dist/lib/helpers/sanitize.d.mts +95 -94
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -1
- package/types/dist/lib/helpers/utilities.d.mts +53 -116
- package/types/dist/lib/helpers/utilities.d.mts.map +1 -1
- package/types/dist/lib/i18n/translations.d.mts +39 -0
- package/types/dist/lib/i18n/translations.d.mts.map +1 -0
- package/types/dist/lib/modes/eager.d.mts +36 -0
- package/types/dist/lib/modes/eager.d.mts.map +1 -0
- package/types/dist/lib/modes/lazy.d.mts +49 -0
- package/types/dist/lib/modes/lazy.d.mts.map +1 -0
- package/types/dist/lib/processors/flatten.d.mts +114 -0
- package/types/dist/lib/processors/flatten.d.mts.map +1 -0
- package/types/dist/lib/processors/loader.d.mts +47 -0
- package/types/dist/lib/processors/loader.d.mts.map +1 -0
- package/types/dist/lib/processors/type-generator.d.mts +19 -0
- package/types/dist/lib/processors/type-generator.d.mts.map +1 -0
- package/types/dist/lib/processors/typescript.d.mts +55 -0
- package/types/dist/lib/processors/typescript.d.mts.map +1 -0
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts +47 -42
- package/types/dist/lib/runtime/runtime-asynclocalstorage.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime-livebindings.d.mts +34 -65
- package/types/dist/lib/runtime/runtime-livebindings.d.mts.map +1 -1
- package/types/dist/lib/runtime/runtime.d.mts +39 -9
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -1
- package/types/dist/slothlet.d.mts +184 -111
- package/types/dist/slothlet.d.mts.map +1 -1
- package/types/index.d.mts +1 -3
- package/dist/lib/engine/README.md +0 -21
- package/dist/lib/engine/slothlet_child.mjs +0 -59
- package/dist/lib/engine/slothlet_engine.mjs +0 -372
- package/dist/lib/engine/slothlet_esm.mjs +0 -230
- package/dist/lib/engine/slothlet_helpers.mjs +0 -455
- package/dist/lib/engine/slothlet_worker.mjs +0 -149
- package/dist/lib/helpers/als-eventemitter.mjs +0 -256
- package/dist/lib/helpers/api_builder/add_api.mjs +0 -553
- package/dist/lib/helpers/api_builder/analysis.mjs +0 -532
- package/dist/lib/helpers/api_builder/construction.mjs +0 -495
- package/dist/lib/helpers/api_builder/decisions.mjs +0 -748
- package/dist/lib/helpers/api_builder/metadata.mjs +0 -248
- package/dist/lib/helpers/api_builder.mjs +0 -41
- package/dist/lib/helpers/auto-wrap.mjs +0 -62
- package/dist/lib/helpers/hooks.mjs +0 -389
- package/dist/lib/helpers/instance-manager.mjs +0 -111
- package/dist/lib/helpers/metadata-api.mjs +0 -201
- package/dist/lib/helpers/multidefault.mjs +0 -216
- package/dist/lib/modes/slothlet_eager.mjs +0 -154
- package/dist/lib/modes/slothlet_lazy.mjs +0 -594
- package/docs/API-RULES-CONDITIONS.md +0 -712
- package/types/dist/lib/engine/slothlet_child.d.mts +0 -2
- package/types/dist/lib/engine/slothlet_child.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_engine.d.mts +0 -31
- package/types/dist/lib/engine/slothlet_engine.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_esm.d.mts +0 -19
- package/types/dist/lib/engine/slothlet_esm.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_helpers.d.mts +0 -25
- package/types/dist/lib/engine/slothlet_helpers.d.mts.map +0 -1
- package/types/dist/lib/engine/slothlet_worker.d.mts +0 -2
- package/types/dist/lib/engine/slothlet_worker.d.mts.map +0 -1
- package/types/dist/lib/helpers/als-eventemitter.d.mts +0 -56
- package/types/dist/lib/helpers/als-eventemitter.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/add_api.d.mts +0 -102
- package/types/dist/lib/helpers/api_builder/add_api.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/analysis.d.mts +0 -189
- package/types/dist/lib/helpers/api_builder/analysis.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/construction.d.mts +0 -107
- package/types/dist/lib/helpers/api_builder/construction.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/decisions.d.mts +0 -213
- package/types/dist/lib/helpers/api_builder/decisions.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder/metadata.d.mts +0 -99
- package/types/dist/lib/helpers/api_builder/metadata.d.mts.map +0 -1
- package/types/dist/lib/helpers/api_builder.d.mts +0 -6
- package/types/dist/lib/helpers/api_builder.d.mts.map +0 -1
- package/types/dist/lib/helpers/auto-wrap.d.mts +0 -49
- package/types/dist/lib/helpers/auto-wrap.d.mts.map +0 -1
- package/types/dist/lib/helpers/hooks.d.mts +0 -342
- package/types/dist/lib/helpers/hooks.d.mts.map +0 -1
- package/types/dist/lib/helpers/instance-manager.d.mts +0 -41
- package/types/dist/lib/helpers/instance-manager.d.mts.map +0 -1
- package/types/dist/lib/helpers/metadata-api.d.mts +0 -132
- package/types/dist/lib/helpers/metadata-api.d.mts.map +0 -1
- package/types/dist/lib/helpers/multidefault.d.mts +0 -90
- package/types/dist/lib/helpers/multidefault.d.mts.map +0 -1
- package/types/dist/lib/modes/slothlet_eager.d.mts +0 -65
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +0 -1
- package/types/dist/lib/modes/slothlet_lazy.d.mts +0 -31
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +0 -1
- package/types/index.d.mts.map +0 -1
|
@@ -0,0 +1,184 @@
|
|
|
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
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
21
|
+
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
22
|
+
import { runtime_isClassInstance, runtime_wrapClassInstance } from "@cldmv/slothlet/helpers/class-instance-wrapper";
|
|
23
|
+
import { setApiContextChecker } from "@cldmv/slothlet/helpers/eventemitter-context";
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export class AsyncContextManager {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.als = new AsyncLocalStorage();
|
|
29
|
+
this.instances = new Map();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
registerEventEmitterContextChecker() {
|
|
34
|
+
setApiContextChecker(() => {
|
|
35
|
+
const store = this.als.getStore();
|
|
36
|
+
return store && store.instanceID ? true : false;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
initialize(instanceID, config = {}) {
|
|
42
|
+
if (this.instances.has(instanceID)) {
|
|
43
|
+
throw new SlothletError("CONTEXT_ALREADY_EXISTS", { instanceID }, null, { validationError: true });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const store = {
|
|
47
|
+
instanceID,
|
|
48
|
+
self: {},
|
|
49
|
+
context: {},
|
|
50
|
+
config: { ...config },
|
|
51
|
+
createdAt: Date.now()
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
this.instances.set(instanceID, store);
|
|
55
|
+
return store;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
runInContext(instanceID, fn, thisArg, args, currentWrapper) {
|
|
60
|
+
|
|
61
|
+
const activeStore = this.als.getStore();
|
|
62
|
+
let baseStore;
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
const isActiveOurInstance = activeStore && (activeStore.instanceID === instanceID || activeStore.parentInstanceID === instanceID);
|
|
66
|
+
|
|
67
|
+
if (isActiveOurInstance) {
|
|
68
|
+
|
|
69
|
+
baseStore = activeStore;
|
|
70
|
+
} else {
|
|
71
|
+
|
|
72
|
+
baseStore = this.instances.get(instanceID);
|
|
73
|
+
if (!baseStore) {
|
|
74
|
+
throw new SlothletError("CONTEXT_NOT_FOUND", {
|
|
75
|
+
instanceID,
|
|
76
|
+
availableInstances: Array.from(this.instances.keys())
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
const executionStore = { ...baseStore };
|
|
83
|
+
if (currentWrapper) {
|
|
84
|
+
executionStore.callerWrapper = baseStore.currentWrapper;
|
|
85
|
+
executionStore.currentWrapper = currentWrapper;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
if (isActiveOurInstance) {
|
|
90
|
+
|
|
91
|
+
return this.als.run(executionStore, () => {
|
|
92
|
+
try {
|
|
93
|
+
const result = fn.apply(thisArg, args);
|
|
94
|
+
|
|
95
|
+
if (runtime_isClassInstance(result)) {
|
|
96
|
+
const instanceCache = new WeakMap();
|
|
97
|
+
return runtime_wrapClassInstance(result, this, instanceID, instanceCache);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
} catch (error) {
|
|
101
|
+
throw new SlothletError(
|
|
102
|
+
"CONTEXT_EXECUTION_FAILED",
|
|
103
|
+
{
|
|
104
|
+
instanceID
|
|
105
|
+
},
|
|
106
|
+
error
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
return this.als.run(executionStore, () => {
|
|
114
|
+
try {
|
|
115
|
+
const result = fn.apply(thisArg, args);
|
|
116
|
+
|
|
117
|
+
if (runtime_isClassInstance(result)) {
|
|
118
|
+
const instanceCache = new WeakMap();
|
|
119
|
+
return runtime_wrapClassInstance(result, this, instanceID, instanceCache);
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw new SlothletError(
|
|
124
|
+
"CONTEXT_EXECUTION_FAILED",
|
|
125
|
+
{
|
|
126
|
+
instanceID
|
|
127
|
+
},
|
|
128
|
+
error
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
getContext() {
|
|
136
|
+
const store = this.als.getStore();
|
|
137
|
+
if (!store) {
|
|
138
|
+
throw new SlothletError("NO_ACTIVE_CONTEXT_ASYNC", {}, null, { validationError: true });
|
|
139
|
+
}
|
|
140
|
+
return store;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
tryGetContext() {
|
|
145
|
+
return this.als.getStore();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
cleanup(instanceID) {
|
|
150
|
+
const store = this.instances.get(instanceID);
|
|
151
|
+
if (!store) {
|
|
152
|
+
throw new SlothletError(
|
|
153
|
+
"CONTEXT_NOT_FOUND",
|
|
154
|
+
{ instanceID, availableInstances: Array.from(this.instances.keys()).join(", ") || "none" },
|
|
155
|
+
null,
|
|
156
|
+
{ validationError: true }
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
store.self = {};
|
|
162
|
+
store.context = {};
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
this.instances.delete(instanceID);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
getDiagnostics() {
|
|
170
|
+
return {
|
|
171
|
+
type: "async",
|
|
172
|
+
activeStore: this.als.getStore()?.instanceID || null,
|
|
173
|
+
instances: Array.from(this.instances.entries()).map(([id, store]) => ({
|
|
174
|
+
id,
|
|
175
|
+
createdAt: store.createdAt,
|
|
176
|
+
contextKeys: Object.keys(store.context),
|
|
177
|
+
selfKeys: Object.keys(store.self)
|
|
178
|
+
}))
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
export const asyncContextManager = new AsyncContextManager();
|
|
@@ -0,0 +1,184 @@
|
|
|
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
|
+
import { SlothletError } from "@cldmv/slothlet/errors";
|
|
21
|
+
import { setApiContextChecker } from "@cldmv/slothlet/helpers/eventemitter-context";
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export class LiveContextManager {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.instances = new Map();
|
|
27
|
+
this.currentInstanceID = null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
registerEventEmitterContextChecker() {
|
|
32
|
+
setApiContextChecker(() => {
|
|
33
|
+
return this.currentInstanceID !== null;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
initialize(instanceID, config = {}) {
|
|
39
|
+
if (this.instances.has(instanceID)) {
|
|
40
|
+
throw new SlothletError("CONTEXT_ALREADY_EXISTS", { instanceID }, null, { validationError: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const store = {
|
|
44
|
+
instanceID,
|
|
45
|
+
self: {},
|
|
46
|
+
context: {},
|
|
47
|
+
config: { ...config },
|
|
48
|
+
createdAt: Date.now()
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
this.instances.set(instanceID, store);
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if (!this.currentInstanceID) {
|
|
55
|
+
this.currentInstanceID = instanceID;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return store;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
runInContext(instanceID, fn, thisArg, args, currentWrapper) {
|
|
63
|
+
|
|
64
|
+
const currentID = this.currentInstanceID;
|
|
65
|
+
let isAlreadyInContext = false;
|
|
66
|
+
|
|
67
|
+
if (currentID) {
|
|
68
|
+
const currentStore = this.instances.get(currentID);
|
|
69
|
+
isAlreadyInContext =
|
|
70
|
+
currentID === instanceID || currentStore?.parentInstanceID === instanceID || currentID.startsWith(instanceID + "__run_");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
const targetInstanceID = isAlreadyInContext ? currentID : instanceID;
|
|
75
|
+
|
|
76
|
+
const store = this.instances.get(targetInstanceID);
|
|
77
|
+
if (!store) {
|
|
78
|
+
throw new SlothletError("CONTEXT_NOT_FOUND", {
|
|
79
|
+
instanceID: targetInstanceID,
|
|
80
|
+
availableInstances: Array.from(this.instances.keys())
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
const previousInstanceID = this.currentInstanceID;
|
|
86
|
+
const previousWrapper = store.currentWrapper;
|
|
87
|
+
const previousCallerWrapper = store.callerWrapper;
|
|
88
|
+
|
|
89
|
+
this.currentInstanceID = targetInstanceID;
|
|
90
|
+
if (currentWrapper) {
|
|
91
|
+
store.callerWrapper = previousWrapper;
|
|
92
|
+
store.currentWrapper = currentWrapper;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
return fn.apply(thisArg, args);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new SlothletError(
|
|
99
|
+
"CONTEXT_EXECUTION_FAILED",
|
|
100
|
+
{
|
|
101
|
+
instanceID
|
|
102
|
+
},
|
|
103
|
+
error
|
|
104
|
+
);
|
|
105
|
+
} finally {
|
|
106
|
+
|
|
107
|
+
this.currentInstanceID = previousInstanceID;
|
|
108
|
+
store.currentWrapper = previousWrapper;
|
|
109
|
+
store.callerWrapper = previousCallerWrapper;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
getContext() {
|
|
115
|
+
if (!this.currentInstanceID) {
|
|
116
|
+
throw new SlothletError("NO_ACTIVE_CONTEXT_LIVE", {}, null, { validationError: true });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const store = this.instances.get(this.currentInstanceID);
|
|
120
|
+
if (!store) {
|
|
121
|
+
throw new SlothletError(
|
|
122
|
+
"CONTEXT_NOT_FOUND",
|
|
123
|
+
{
|
|
124
|
+
instanceID: this.currentInstanceID,
|
|
125
|
+
availableInstances: Array.from(this.instances.keys()).join(", ") || "none"
|
|
126
|
+
},
|
|
127
|
+
null,
|
|
128
|
+
{ validationError: true }
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return store;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
tryGetContext() {
|
|
137
|
+
if (!this.currentInstanceID) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
return this.instances.get(this.currentInstanceID);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
cleanup(instanceID) {
|
|
145
|
+
const store = this.instances.get(instanceID);
|
|
146
|
+
if (!store) {
|
|
147
|
+
throw new SlothletError(
|
|
148
|
+
"CONTEXT_NOT_FOUND",
|
|
149
|
+
{ instanceID, availableInstances: Array.from(this.instances.keys()).join(", ") || "none" },
|
|
150
|
+
null,
|
|
151
|
+
{ validationError: true }
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
store.self = {};
|
|
157
|
+
store.context = {};
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
this.instances.delete(instanceID);
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
if (this.currentInstanceID === instanceID) {
|
|
164
|
+
this.currentInstanceID = null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
getDiagnostics() {
|
|
170
|
+
return {
|
|
171
|
+
type: "live",
|
|
172
|
+
currentInstanceID: this.currentInstanceID,
|
|
173
|
+
instances: Array.from(this.instances.entries()).map(([id, store]) => ({
|
|
174
|
+
id,
|
|
175
|
+
createdAt: store.createdAt,
|
|
176
|
+
contextKeys: Object.keys(store.context),
|
|
177
|
+
selfKeys: Object.keys(store.self)
|
|
178
|
+
}))
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
export const liveContextManager = new LiveContextManager();
|