@arch-cadre/modules 0.0.54 → 0.0.56
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/dist/_virtual/_rolldown/runtime.cjs +1 -0
- package/dist/client/extension-point-client.cjs +1 -35
- package/dist/client/extension-point-client.mjs +1 -22
- package/dist/client/extension-point.cjs +1 -26
- package/dist/client/extension-point.mjs +1 -15
- package/dist/client/widget-area.cjs +1 -30
- package/dist/client/widget-area.mjs +1 -17
- package/dist/index.cjs +1 -43
- package/dist/index.mjs +1 -4
- package/dist/server/lifecycle.cjs +1 -217
- package/dist/server/lifecycle.mjs +1 -238
- package/dist/server/manage.cjs +1 -140
- package/dist/server/manage.mjs +1 -126
- package/dist/server/registry.cjs +1 -94
- package/dist/server/registry.mjs +1 -99
- package/dist/server/ui.cjs +1 -222
- package/dist/server/ui.mjs +1 -218
- package/dist/server.cjs +1 -60
- package/dist/server.mjs +1 -5
- package/dist/types.cjs +1 -20
- package/dist/types.mjs +1 -14
- package/package.json +6 -6
- package/dist/client/extension-point-client.d.ts +0 -10
- package/dist/client/extension-point.d.ts +0 -9
- package/dist/client/index.cjs +0 -38
- package/dist/client/index.d.ts +0 -3
- package/dist/client/index.mjs +0 -3
- package/dist/client/widget-area.d.ts +0 -9
- package/dist/index.d.ts +0 -4
- package/dist/server/lifecycle.d.ts +0 -5
- package/dist/server/manage.d.ts +0 -12
- package/dist/server/registry.d.ts +0 -1
- package/dist/server/ui.d.ts +0 -12
- package/dist/server.d.ts +0 -5
- package/dist/types.d.ts +0 -113
|
@@ -1,238 +1 @@
|
|
|
1
|
-
"use server";
|
|
2
|
-
import { exec } from "node:child_process";
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { promisify } from "node:util";
|
|
6
|
-
import { systemModulesTable } from "@arch-cadre/core";
|
|
7
|
-
import { db } from "@arch-cadre/core/server";
|
|
8
|
-
import { eq } from "drizzle-orm";
|
|
9
|
-
import { getModuleInstance, getModules } from "./manage.mjs";
|
|
10
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
-
async function updateStep(moduleId, step) {
|
|
12
|
-
console.log(`[Kernel] "${moduleId}" step: ${step}`);
|
|
13
|
-
await db.update(systemModulesTable).set({ lastStep: step }).where(eq(systemModulesTable.id, moduleId));
|
|
14
|
-
}
|
|
15
|
-
async function resolveSchemaPath(moduleId) {
|
|
16
|
-
const root = process.cwd();
|
|
17
|
-
if (moduleId === "@arch-cadre/core" || moduleId === "core") {
|
|
18
|
-
const p = path.join(
|
|
19
|
-
root,
|
|
20
|
-
"node_modules",
|
|
21
|
-
"@kryo",
|
|
22
|
-
"core",
|
|
23
|
-
"dist",
|
|
24
|
-
"server",
|
|
25
|
-
"database",
|
|
26
|
-
"schema.cjs"
|
|
27
|
-
);
|
|
28
|
-
try {
|
|
29
|
-
await fs.access(p);
|
|
30
|
-
return p;
|
|
31
|
-
} catch {
|
|
32
|
-
const devP = path.resolve(
|
|
33
|
-
root,
|
|
34
|
-
"../../packages/kryo-core/src/server/database/schema.ts"
|
|
35
|
-
);
|
|
36
|
-
try {
|
|
37
|
-
await fs.access(devP);
|
|
38
|
-
return devP;
|
|
39
|
-
} catch {
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
const cleanId = moduleId.replace("@arch-cadre/", "");
|
|
45
|
-
const possible = [
|
|
46
|
-
// Local module
|
|
47
|
-
path.join(root, "modules", cleanId, "schema.ts"),
|
|
48
|
-
path.join(root, "modules", moduleId, "schema.ts"),
|
|
49
|
-
// node_modules (installed or symlinked)
|
|
50
|
-
path.join(root, "node_modules", moduleId, "dist", "schema.cjs"),
|
|
51
|
-
path.join(
|
|
52
|
-
root,
|
|
53
|
-
"node_modules",
|
|
54
|
-
`@arch-cadre/${cleanId}`,
|
|
55
|
-
"dist",
|
|
56
|
-
"schema.cjs"
|
|
57
|
-
),
|
|
58
|
-
path.join(root, "node_modules", moduleId, "src", "schema.ts"),
|
|
59
|
-
path.join(
|
|
60
|
-
root,
|
|
61
|
-
"node_modules",
|
|
62
|
-
`@arch-cadre/${cleanId}`,
|
|
63
|
-
"src",
|
|
64
|
-
"schema.ts"
|
|
65
|
-
),
|
|
66
|
-
// Monorepo packages (searching by various naming conventions)
|
|
67
|
-
path.resolve(root, "../../packages", cleanId, "src", "schema.ts"),
|
|
68
|
-
path.resolve(root, "../../packages", `kryo-${cleanId}`, "src", "schema.ts"),
|
|
69
|
-
path.resolve(root, "../../packages", cleanId, "schema.ts")
|
|
70
|
-
];
|
|
71
|
-
for (const p of possible) {
|
|
72
|
-
try {
|
|
73
|
-
await fs.access(p);
|
|
74
|
-
console.log(
|
|
75
|
-
`[Kernel:Lifecycle] Resolved schema for ${moduleId} at: ${p}`
|
|
76
|
-
);
|
|
77
|
-
return p;
|
|
78
|
-
} catch {
|
|
79
|
-
console.warn(
|
|
80
|
-
`[Kernel:Lifecycle] Unresolved schema for ${moduleId} at: ${p}`
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
console.warn(
|
|
85
|
-
`[Kernel:Lifecycle] Could not resolve schema path for: ${moduleId}`
|
|
86
|
-
);
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
export async function pushModuleSchema(moduleId) {
|
|
90
|
-
const execAsync = promisify(exec);
|
|
91
|
-
console.log(`[Kernel:Lifecycle] Targeted sync for module: ${moduleId}`);
|
|
92
|
-
const root = process.cwd();
|
|
93
|
-
const enabledFromDb = await db.select({ id: systemModulesTable.id }).from(systemModulesTable).where(eq(systemModulesTable.enabled, true));
|
|
94
|
-
const activeModuleIds = new Set(enabledFromDb.map((m) => m.id));
|
|
95
|
-
activeModuleIds.add("@arch-cadre/core");
|
|
96
|
-
activeModuleIds.add(moduleId);
|
|
97
|
-
const schemaPaths = [];
|
|
98
|
-
for (const id of Array.from(activeModuleIds)) {
|
|
99
|
-
const p = await resolveSchemaPath(id);
|
|
100
|
-
if (p) schemaPaths.push(p);
|
|
101
|
-
}
|
|
102
|
-
if (schemaPaths.length === 0) {
|
|
103
|
-
console.warn(
|
|
104
|
-
`[Kernel:Lifecycle] No schema paths resolved for ${moduleId}, skipping push.`
|
|
105
|
-
);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
const configPath = path.join(root, "drizzle.config.ts");
|
|
109
|
-
const drizzleKitBin = path.join(root, "node_modules", ".bin", "drizzle-kit");
|
|
110
|
-
const cmd = `"${drizzleKitBin}" push --force --config=${configPath}`;
|
|
111
|
-
console.log(
|
|
112
|
-
`[Kernel:Lifecycle] Executing isolated migration. Modules: ${Array.from(activeModuleIds).join(", ")}`
|
|
113
|
-
);
|
|
114
|
-
try {
|
|
115
|
-
await execAsync(cmd, {
|
|
116
|
-
env: {
|
|
117
|
-
...process.env,
|
|
118
|
-
CI: "true",
|
|
119
|
-
// Pass the calculated schemas to our dynamic drizzle.config.ts
|
|
120
|
-
DRIZZLE_SCHEMA_OVERRIDE: schemaPaths.join(",")
|
|
121
|
-
},
|
|
122
|
-
cwd: root
|
|
123
|
-
});
|
|
124
|
-
console.log(
|
|
125
|
-
`[Kernel:Lifecycle] Isolated migration successful for ${moduleId}`
|
|
126
|
-
);
|
|
127
|
-
} catch (e) {
|
|
128
|
-
console.error(
|
|
129
|
-
`[Kernel:Lifecycle] Isolated migration failed for ${moduleId}:`
|
|
130
|
-
);
|
|
131
|
-
console.error(e.stdout || e.message);
|
|
132
|
-
throw new Error(`Migration failed: ${moduleId}`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
async function performToggle(moduleId, isEnabled) {
|
|
136
|
-
try {
|
|
137
|
-
const allModules = await getModules();
|
|
138
|
-
const manifest = allModules.find((m) => m.id === moduleId);
|
|
139
|
-
await updateStep(moduleId, "Validate module...");
|
|
140
|
-
await sleep(500);
|
|
141
|
-
if (!manifest) return;
|
|
142
|
-
if (isEnabled) {
|
|
143
|
-
await updateStep(moduleId, "Queued...");
|
|
144
|
-
await sleep(500);
|
|
145
|
-
if (manifest.dependencies?.length) {
|
|
146
|
-
const pendingDeps = manifest.dependencies.filter((depId) => {
|
|
147
|
-
const dep = allModules.find((m) => m.id === depId);
|
|
148
|
-
return dep && !dep.enabled && !dep.system;
|
|
149
|
-
});
|
|
150
|
-
if (pendingDeps.length > 0) {
|
|
151
|
-
for (const depId of pendingDeps) {
|
|
152
|
-
await updateStep(moduleId, `Waiting for dependency "${depId}"...`);
|
|
153
|
-
await performToggle(depId, true);
|
|
154
|
-
await sleep(500);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
await updateStep(moduleId, "Initializing...");
|
|
159
|
-
await sleep(500);
|
|
160
|
-
await db.update(systemModulesTable).set({ enabled: true }).where(eq(systemModulesTable.id, moduleId));
|
|
161
|
-
const instance = await getModuleInstance(moduleId);
|
|
162
|
-
await updateStep(moduleId, "Migrate database...");
|
|
163
|
-
await sleep(500);
|
|
164
|
-
try {
|
|
165
|
-
if (instance?.onMigrate) {
|
|
166
|
-
await instance.onMigrate();
|
|
167
|
-
} else {
|
|
168
|
-
await pushModuleSchema(moduleId);
|
|
169
|
-
}
|
|
170
|
-
await updateStep(moduleId, "Migration successful");
|
|
171
|
-
} catch (e) {
|
|
172
|
-
console.error(`[Kernel] Migration failed for ${moduleId}:`, e);
|
|
173
|
-
await updateStep(moduleId, `Migration failed: ${e.message}`);
|
|
174
|
-
await sleep(500);
|
|
175
|
-
}
|
|
176
|
-
if (instance?.onEnable) {
|
|
177
|
-
await updateStep(moduleId, "Running setup...");
|
|
178
|
-
await sleep(500);
|
|
179
|
-
await instance.onEnable();
|
|
180
|
-
}
|
|
181
|
-
await updateStep(moduleId, "Finishing...");
|
|
182
|
-
await sleep(500);
|
|
183
|
-
await db.update(systemModulesTable).set({ installed: true, lastStep: null }).where(eq(systemModulesTable.id, moduleId));
|
|
184
|
-
} else {
|
|
185
|
-
const dependents = allModules.filter((m) => {
|
|
186
|
-
if (!m.enabled || m.id === moduleId) return false;
|
|
187
|
-
if (m.dependencies?.includes(moduleId)) return true;
|
|
188
|
-
if (m.extends?.includes(moduleId)) {
|
|
189
|
-
const otherActiveTargets = m.extends.filter((targetId) => {
|
|
190
|
-
if (targetId === moduleId) return false;
|
|
191
|
-
const target = allModules.find((mod) => mod.id === targetId);
|
|
192
|
-
return target?.enabled;
|
|
193
|
-
});
|
|
194
|
-
return otherActiveTargets.length === 0;
|
|
195
|
-
}
|
|
196
|
-
return false;
|
|
197
|
-
});
|
|
198
|
-
if (dependents.length > 0) {
|
|
199
|
-
await updateStep(moduleId, `Deactivating dependents...`);
|
|
200
|
-
for (const dep of dependents) {
|
|
201
|
-
await performToggle(dep.id, false);
|
|
202
|
-
await sleep(500);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
await updateStep(moduleId, "Deactivating...");
|
|
206
|
-
const instance = await getModuleInstance(moduleId);
|
|
207
|
-
if (instance?.onDisable) await instance.onDisable();
|
|
208
|
-
await db.update(systemModulesTable).set({ enabled: false, installed: false }).where(eq(systemModulesTable.id, moduleId));
|
|
209
|
-
await updateStep(moduleId, null);
|
|
210
|
-
}
|
|
211
|
-
} catch (e) {
|
|
212
|
-
console.error(`[Kernel] Fatal error for ${moduleId}:`, e);
|
|
213
|
-
await updateStep(moduleId, `Error: ${e.message}`);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
export async function toggleModuleState(moduleId, isEnabled) {
|
|
217
|
-
const allModules = await getModules();
|
|
218
|
-
const manifest = allModules.find((m) => m.id === moduleId);
|
|
219
|
-
if (!manifest) throw new Error(`Module "${moduleId}" not found`);
|
|
220
|
-
if (manifest.enabled === isEnabled) return { success: true };
|
|
221
|
-
void performToggle(moduleId, isEnabled);
|
|
222
|
-
return { success: true };
|
|
223
|
-
}
|
|
224
|
-
export async function initOperationalModules() {
|
|
225
|
-
const allModules = await getModules();
|
|
226
|
-
for (const mod of allModules) {
|
|
227
|
-
if (mod.enabled) {
|
|
228
|
-
try {
|
|
229
|
-
const instance = await getModuleInstance(mod.id);
|
|
230
|
-
if (instance?.init) {
|
|
231
|
-
await instance.init();
|
|
232
|
-
}
|
|
233
|
-
} catch (e) {
|
|
234
|
-
console.error(`[Kernel] Failed to init module ${mod.id}:`, e);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
1
|
+
"use server";import{getModuleInstance as e,getModules as t}from"./manage.mjs";import{db as n}from"@arch-cadre/core/server";import r from"node:fs/promises";import i from"node:path";import{systemModulesTable as a}from"@arch-cadre/core";import{eq as o}from"drizzle-orm";import{exec as s}from"node:child_process";import{promisify as c}from"node:util";const l=e=>new Promise(t=>setTimeout(t,e));async function u(e,t){console.log(`[Kernel] "${e}" step: ${t}`),await n.update(a).set({lastStep:t}).where(o(a.id,e))}async function d(e){let t=process.cwd();if(e===`@arch-cadre/core`||e===`core`){let e=i.join(t,`node_modules`,`@kryo`,`core`,`dist`,`server`,`database`,`schema.cjs`);try{return await r.access(e),e}catch{let e=i.resolve(t,`../../packages/kryo-core/src/server/database/schema.ts`);try{return await r.access(e),e}catch{}}return null}let n=e.replace(`@arch-cadre/`,``),a=[i.join(t,`modules`,n,`schema.ts`),i.join(t,`modules`,e,`schema.ts`),i.join(t,`node_modules`,e,`dist`,`schema.cjs`),i.join(t,`node_modules`,`@arch-cadre/${n}`,`dist`,`schema.cjs`),i.join(t,`node_modules`,e,`src`,`schema.ts`),i.join(t,`node_modules`,`@arch-cadre/${n}`,`src`,`schema.ts`),i.resolve(t,`../../packages`,n,`src`,`schema.ts`),i.resolve(t,`../../packages`,`kryo-${n}`,`src`,`schema.ts`),i.resolve(t,`../../packages`,n,`schema.ts`)];for(let t of a)try{return await r.access(t),console.log(`[Kernel:Lifecycle] Resolved schema for ${e} at: ${t}`),t}catch{console.warn(`[Kernel:Lifecycle] Unresolved schema for ${e} at: ${t}`)}return console.warn(`[Kernel:Lifecycle] Could not resolve schema path for: ${e}`),null}async function f(e){let t=c(s);console.log(`[Kernel:Lifecycle] Targeted sync for module: ${e}`);let r=process.cwd(),l=await n.select({id:a.id}).from(a).where(o(a.enabled,!0)),u=new Set(l.map(e=>e.id));u.add(`@arch-cadre/core`),u.add(e);let f=[];for(let e of Array.from(u)){let t=await d(e);t&&f.push(t)}if(f.length===0){console.warn(`[Kernel:Lifecycle] No schema paths resolved for ${e}, skipping push.`);return}let p=i.join(r,`drizzle.config.ts`),m=`"${i.join(r,`node_modules`,`.bin`,`drizzle-kit`)}" push --force --config=${p}`;console.log(`[Kernel:Lifecycle] Executing isolated migration. Modules: ${Array.from(u).join(`, `)}`);try{await t(m,{env:{...process.env,CI:`true`,DRIZZLE_SCHEMA_OVERRIDE:f.join(`,`)},cwd:r}),console.log(`[Kernel:Lifecycle] Isolated migration successful for ${e}`)}catch(t){throw console.error(`[Kernel:Lifecycle] Isolated migration failed for ${e}:`),console.error(t.stdout||t.message),Error(`Migration failed: ${e}`)}}async function p(r,i){try{let s=await t(),c=s.find(e=>e.id===r);if(await u(r,`Validate module...`),await l(500),!c)return;if(i){if(await u(r,`Queued...`),await l(500),c.dependencies?.length){let e=c.dependencies.filter(e=>{let t=s.find(t=>t.id===e);return t&&!t.enabled&&!t.system});if(e.length>0)for(let t of e)await u(r,`Waiting for dependency "${t}"...`),await p(t,!0),await l(500)}await u(r,`Initializing...`),await l(500),await n.update(a).set({enabled:!0}).where(o(a.id,r));let t=await e(r);await u(r,`Migrate database...`),await l(500);try{t?.onMigrate?await t.onMigrate():await f(r),await u(r,`Migration successful`)}catch(e){console.error(`[Kernel] Migration failed for ${r}:`,e),await u(r,`Migration failed: ${e.message}`),await l(500)}t?.onEnable&&(await u(r,`Running setup...`),await l(500),await t.onEnable()),await u(r,`Finishing...`),await l(500),await n.update(a).set({installed:!0,lastStep:null}).where(o(a.id,r))}else{let t=s.filter(e=>!e.enabled||e.id===r?!1:e.dependencies?.includes(r)?!0:e.extends?.includes(r)?e.extends.filter(e=>e===r?!1:s.find(t=>t.id===e)?.enabled).length===0:!1);if(t.length>0){await u(r,`Deactivating dependents...`);for(let e of t)await p(e.id,!1),await l(500)}await u(r,`Deactivating...`);let i=await e(r);i?.onDisable&&await i.onDisable(),await n.update(a).set({enabled:!1,installed:!1}).where(o(a.id,r)),await u(r,null)}}catch(e){console.error(`[Kernel] Fatal error for ${r}:`,e),await u(r,`Error: ${e.message}`)}}async function m(e,n){let r=(await t()).find(t=>t.id===e);if(!r)throw Error(`Module "${e}" not found`);return r.enabled===n||p(e,n),{success:!0}}async function h(){let n=await t();for(let t of n)if(t.enabled)try{let n=await e(t.id);n?.init&&await n.init()}catch(e){console.error(`[Kernel] Failed to init module ${t.id}:`,e)}}export{h as initOperationalModules,f as pushModuleSchema,m as toggleModuleState};
|
package/dist/server/manage.cjs
CHANGED
|
@@ -1,140 +1 @@
|
|
|
1
|
-
"use
|
|
2
|
-
"use server";
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.getModuleConfig = getModuleConfig;
|
|
8
|
-
exports.getModuleInstance = getModuleInstance;
|
|
9
|
-
exports.getModuleStatus = getModuleStatus;
|
|
10
|
-
exports.getModules = getModules;
|
|
11
|
-
exports.isModuleEnabled = isModuleEnabled;
|
|
12
|
-
exports.registerModules = registerModules;
|
|
13
|
-
exports.updateModuleConfig = updateModuleConfig;
|
|
14
|
-
var _promises = _interopRequireDefault(require("node:fs/promises"));
|
|
15
|
-
var _nodePath = _interopRequireDefault(require("node:path"));
|
|
16
|
-
var _core = require("@arch-cadre/core");
|
|
17
|
-
var _server = require("@arch-cadre/core/server");
|
|
18
|
-
var _drizzleOrm = require("drizzle-orm");
|
|
19
|
-
var _types = require("../types.cjs");
|
|
20
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
21
|
-
const globalForModules = globalThis;
|
|
22
|
-
if (!globalForModules.__KRYO_REGISTERED_MODULES__) {
|
|
23
|
-
globalForModules.__KRYO_REGISTERED_MODULES__ = /* @__PURE__ */new Map();
|
|
24
|
-
}
|
|
25
|
-
async function registerModules(modules) {
|
|
26
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
27
|
-
for (const mod of modules) {
|
|
28
|
-
if (mod.manifest?.id) {
|
|
29
|
-
store.set(mod.manifest.id, mod);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async function getModules() {
|
|
34
|
-
try {
|
|
35
|
-
const modulesDir = await (0, _server.getModulesDir)();
|
|
36
|
-
const manifests = [];
|
|
37
|
-
const entries = await _promises.default.readdir(modulesDir).catch(() => []);
|
|
38
|
-
for (const dir of entries) {
|
|
39
|
-
if (dir.startsWith(".")) continue;
|
|
40
|
-
try {
|
|
41
|
-
const content = await _promises.default.readFile(_nodePath.default.join(modulesDir, dir, "manifeston"), "utf-8");
|
|
42
|
-
const manifest = _types.ModuleManifestSchema.parse(JSON.parse(content));
|
|
43
|
-
manifests.push({
|
|
44
|
-
...manifest,
|
|
45
|
-
hasDocs: false
|
|
46
|
-
});
|
|
47
|
-
} catch {}
|
|
48
|
-
}
|
|
49
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
50
|
-
for (const mod of store.values()) {
|
|
51
|
-
if (!manifests.some(m => m.id === mod.manifest.id)) {
|
|
52
|
-
manifests.push({
|
|
53
|
-
...mod.manifest,
|
|
54
|
-
hasDocs: false
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
const dbModules = await _server.db.select().from(_core.systemModulesTable).catch(() => []);
|
|
59
|
-
const dbMap = new Map(dbModules.map(m => [m.id, m]));
|
|
60
|
-
return manifests.map(mod => {
|
|
61
|
-
const dbEntry = dbMap.get(mod.id);
|
|
62
|
-
return {
|
|
63
|
-
...mod,
|
|
64
|
-
enabled: mod.system ? true : dbEntry?.enabled ?? false,
|
|
65
|
-
installed: mod.system ? true : dbEntry?.installed ?? false,
|
|
66
|
-
lastStep: dbEntry?.lastStep,
|
|
67
|
-
hasDocs: mod.hasDocs
|
|
68
|
-
};
|
|
69
|
-
});
|
|
70
|
-
} catch {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async function getModuleStatus(moduleId) {
|
|
75
|
-
try {
|
|
76
|
-
const [mod] = await _server.db.select().from(_core.systemModulesTable).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, moduleId));
|
|
77
|
-
return {
|
|
78
|
-
enabled: mod?.enabled,
|
|
79
|
-
installed: mod?.installed,
|
|
80
|
-
lastStep: mod?.lastStep
|
|
81
|
-
};
|
|
82
|
-
} catch {
|
|
83
|
-
return {
|
|
84
|
-
enabled: false,
|
|
85
|
-
installed: false,
|
|
86
|
-
lastStep: null
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async function isModuleEnabled(moduleId) {
|
|
91
|
-
try {
|
|
92
|
-
const [mod] = await _server.db.select().from(_core.systemModulesTable).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, moduleId));
|
|
93
|
-
return !!mod?.enabled || !!mod?.system;
|
|
94
|
-
} catch {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async function getModuleInstance(moduleId) {
|
|
99
|
-
try {
|
|
100
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
101
|
-
if (store.has(moduleId)) {
|
|
102
|
-
console.log(`[Kernel:Manage] Module "${moduleId}" found in registry.`);
|
|
103
|
-
return store.get(moduleId) || null;
|
|
104
|
-
}
|
|
105
|
-
console.log(`[Kernel:Manage] Module "${moduleId}" not in registry. Registry size: ${store.size}. Keys: ${Array.from(store.keys()).join(", ")}`);
|
|
106
|
-
if (!moduleId.startsWith(".") && !moduleId.startsWith("/")) {
|
|
107
|
-
try {
|
|
108
|
-
const mod = await Promise.resolve(`@arch-cadre/${moduleId}`).then(s => require(s));
|
|
109
|
-
console.log(`[Kernel:Manage] Module "${moduleId}" loaded via import().`);
|
|
110
|
-
return mod.default || mod || null;
|
|
111
|
-
} catch (e) {
|
|
112
|
-
console.warn(`[Kernel:Manage] Failed to import module "${moduleId} via import().": ${e.message}`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return null;
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error(`[Kernel:Manage] Error in getModuleInstance for "${moduleId}":`, error);
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
async function getModuleConfig(moduleId) {
|
|
122
|
-
try {
|
|
123
|
-
const [mod] = await _server.db.select({
|
|
124
|
-
config: _core.systemModulesTable.config
|
|
125
|
-
}).from(_core.systemModulesTable).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, moduleId));
|
|
126
|
-
if (!mod?.config) return null;
|
|
127
|
-
return JSON.parse(mod.config);
|
|
128
|
-
} catch {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
async function updateModuleConfig(moduleId, config) {
|
|
133
|
-
try {
|
|
134
|
-
await _server.db.update(_core.systemModulesTable).set({
|
|
135
|
-
config: JSON.stringify(config)
|
|
136
|
-
}).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, moduleId));
|
|
137
|
-
} catch (e) {
|
|
138
|
-
console.warn(`[Kernel:Manage] Failed to update config for ${moduleId}:`, e);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
1
|
+
"use server";const e=require(`../_virtual/_rolldown/runtime.cjs`),t=require(`../types.cjs`);let n=require(`@arch-cadre/core/server`),r=require(`node:fs/promises`);r=e.__toESM(r);let i=require(`node:path`);i=e.__toESM(i);let a=require(`@arch-cadre/core`),o=require(`drizzle-orm`);const s=globalThis;s.__KRYO_REGISTERED_MODULES__||=new Map;async function c(e){let t=s.__KRYO_REGISTERED_MODULES__;for(let n of e)n.manifest?.id&&t.set(n.manifest.id,n)}async function l(){try{let e=await(0,n.getModulesDir)(),o=[],c=await r.default.readdir(e).catch(()=>[]);for(let n of c)if(!n.startsWith(`.`))try{let a=await r.default.readFile(i.default.join(e,n,`manifeston`),`utf-8`),s=t.ModuleManifestSchema.parse(JSON.parse(a));o.push({...s,hasDocs:!1})}catch{}let l=s.__KRYO_REGISTERED_MODULES__;for(let e of l.values())o.some(t=>t.id===e.manifest.id)||o.push({...e.manifest,hasDocs:!1});let u=await n.db.select().from(a.systemModulesTable).catch(()=>[]),d=new Map(u.map(e=>[e.id,e]));return o.map(e=>{let t=d.get(e.id);return{...e,enabled:e.system?!0:t?.enabled??!1,installed:e.system?!0:t?.installed??!1,lastStep:t?.lastStep,hasDocs:e.hasDocs}})}catch{return[]}}async function u(e){try{let[t]=await n.db.select().from(a.systemModulesTable).where((0,o.eq)(a.systemModulesTable.id,e));return{enabled:t?.enabled,installed:t?.installed,lastStep:t?.lastStep}}catch{return{enabled:!1,installed:!1,lastStep:null}}}async function d(e){try{let[t]=await n.db.select().from(a.systemModulesTable).where((0,o.eq)(a.systemModulesTable.id,e));return!!t?.enabled||!!t?.system}catch{return!1}}async function f(e){try{let t=s.__KRYO_REGISTERED_MODULES__;if(t.has(e))return console.log(`[Kernel:Manage] Module "${e}" found in registry.`),t.get(e)||null;if(console.log(`[Kernel:Manage] Module "${e}" not in registry. Registry size: ${t.size}. Keys: ${Array.from(t.keys()).join(`, `)}`),!e.startsWith(`.`)&&!e.startsWith(`/`))try{let t=await import(`@arch-cadre/${e}`);return console.log(`[Kernel:Manage] Module "${e}" loaded via import().`),t.default||t||null}catch(t){console.warn(`[Kernel:Manage] Failed to import module "${e} via import().": ${t.message}`)}return null}catch(t){return console.error(`[Kernel:Manage] Error in getModuleInstance for "${e}":`,t),null}}async function p(e){try{let[t]=await n.db.select({config:a.systemModulesTable.config}).from(a.systemModulesTable).where((0,o.eq)(a.systemModulesTable.id,e));return t?.config?JSON.parse(t.config):null}catch{return null}}async function m(e,t){try{await n.db.update(a.systemModulesTable).set({config:JSON.stringify(t)}).where((0,o.eq)(a.systemModulesTable.id,e))}catch(t){console.warn(`[Kernel:Manage] Failed to update config for ${e}:`,t)}}exports.getModuleConfig=p,exports.getModuleInstance=f,exports.getModuleStatus=u,exports.getModules=l,exports.isModuleEnabled=d,exports.registerModules=c,exports.updateModuleConfig=m;
|
package/dist/server/manage.mjs
CHANGED
|
@@ -1,126 +1 @@
|
|
|
1
|
-
"use server";
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { systemModulesTable } from "@arch-cadre/core";
|
|
5
|
-
import { db, getModulesDir } from "@arch-cadre/core/server";
|
|
6
|
-
import { eq } from "drizzle-orm";
|
|
7
|
-
import { ModuleManifestSchema } from "../types.mjs";
|
|
8
|
-
const globalForModules = globalThis;
|
|
9
|
-
if (!globalForModules.__KRYO_REGISTERED_MODULES__) {
|
|
10
|
-
globalForModules.__KRYO_REGISTERED_MODULES__ = /* @__PURE__ */ new Map();
|
|
11
|
-
}
|
|
12
|
-
export async function registerModules(modules) {
|
|
13
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
14
|
-
for (const mod of modules) {
|
|
15
|
-
if (mod.manifest?.id) {
|
|
16
|
-
store.set(mod.manifest.id, mod);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export async function getModules() {
|
|
21
|
-
try {
|
|
22
|
-
const modulesDir = await getModulesDir();
|
|
23
|
-
const manifests = [];
|
|
24
|
-
const entries = await fs.readdir(modulesDir).catch(() => []);
|
|
25
|
-
for (const dir of entries) {
|
|
26
|
-
if (dir.startsWith(".")) continue;
|
|
27
|
-
try {
|
|
28
|
-
const content = await fs.readFile(
|
|
29
|
-
path.join(modulesDir, dir, "manifeston"),
|
|
30
|
-
"utf-8"
|
|
31
|
-
);
|
|
32
|
-
const manifest = ModuleManifestSchema.parse(JSON.parse(content));
|
|
33
|
-
manifests.push({ ...manifest, hasDocs: false });
|
|
34
|
-
} catch {
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
38
|
-
for (const mod of store.values()) {
|
|
39
|
-
if (!manifests.some((m) => m.id === mod.manifest.id)) {
|
|
40
|
-
manifests.push({ ...mod.manifest, hasDocs: false });
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
const dbModules = await db.select().from(systemModulesTable).catch(() => []);
|
|
44
|
-
const dbMap = new Map(dbModules.map((m) => [m.id, m]));
|
|
45
|
-
return manifests.map((mod) => {
|
|
46
|
-
const dbEntry = dbMap.get(mod.id);
|
|
47
|
-
return {
|
|
48
|
-
...mod,
|
|
49
|
-
enabled: mod.system ? true : dbEntry?.enabled ?? false,
|
|
50
|
-
installed: mod.system ? true : dbEntry?.installed ?? false,
|
|
51
|
-
lastStep: dbEntry?.lastStep,
|
|
52
|
-
hasDocs: mod.hasDocs
|
|
53
|
-
};
|
|
54
|
-
});
|
|
55
|
-
} catch {
|
|
56
|
-
return [];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
export async function getModuleStatus(moduleId) {
|
|
60
|
-
try {
|
|
61
|
-
const [mod] = await db.select().from(systemModulesTable).where(eq(systemModulesTable.id, moduleId));
|
|
62
|
-
return {
|
|
63
|
-
enabled: mod?.enabled,
|
|
64
|
-
installed: mod?.installed,
|
|
65
|
-
lastStep: mod?.lastStep
|
|
66
|
-
};
|
|
67
|
-
} catch {
|
|
68
|
-
return { enabled: false, installed: false, lastStep: null };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export async function isModuleEnabled(moduleId) {
|
|
72
|
-
try {
|
|
73
|
-
const [mod] = await db.select().from(systemModulesTable).where(eq(systemModulesTable.id, moduleId));
|
|
74
|
-
return !!mod?.enabled || !!mod?.system;
|
|
75
|
-
} catch {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
export async function getModuleInstance(moduleId) {
|
|
80
|
-
try {
|
|
81
|
-
const store = globalForModules.__KRYO_REGISTERED_MODULES__;
|
|
82
|
-
if (store.has(moduleId)) {
|
|
83
|
-
console.log(`[Kernel:Manage] Module "${moduleId}" found in registry.`);
|
|
84
|
-
return store.get(moduleId) || null;
|
|
85
|
-
}
|
|
86
|
-
console.log(
|
|
87
|
-
`[Kernel:Manage] Module "${moduleId}" not in registry. Registry size: ${store.size}. Keys: ${Array.from(store.keys()).join(", ")}`
|
|
88
|
-
);
|
|
89
|
-
if (!moduleId.startsWith(".") && !moduleId.startsWith("/")) {
|
|
90
|
-
try {
|
|
91
|
-
const mod = await import(`@arch-cadre/${moduleId}`);
|
|
92
|
-
console.log(
|
|
93
|
-
`[Kernel:Manage] Module "${moduleId}" loaded via import().`
|
|
94
|
-
);
|
|
95
|
-
return mod.default || mod || null;
|
|
96
|
-
} catch (e) {
|
|
97
|
-
console.warn(
|
|
98
|
-
`[Kernel:Manage] Failed to import module "${moduleId} via import().": ${e.message}`
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return null;
|
|
103
|
-
} catch (error) {
|
|
104
|
-
console.error(
|
|
105
|
-
`[Kernel:Manage] Error in getModuleInstance for "${moduleId}":`,
|
|
106
|
-
error
|
|
107
|
-
);
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
export async function getModuleConfig(moduleId) {
|
|
112
|
-
try {
|
|
113
|
-
const [mod] = await db.select({ config: systemModulesTable.config }).from(systemModulesTable).where(eq(systemModulesTable.id, moduleId));
|
|
114
|
-
if (!mod?.config) return null;
|
|
115
|
-
return JSON.parse(mod.config);
|
|
116
|
-
} catch {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
export async function updateModuleConfig(moduleId, config) {
|
|
121
|
-
try {
|
|
122
|
-
await db.update(systemModulesTable).set({ config: JSON.stringify(config) }).where(eq(systemModulesTable.id, moduleId));
|
|
123
|
-
} catch (e) {
|
|
124
|
-
console.warn(`[Kernel:Manage] Failed to update config for ${moduleId}:`, e);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
1
|
+
"use server";import{ModuleManifestSchema as e}from"../types.mjs";import{db as t,getModulesDir as n}from"@arch-cadre/core/server";import r from"node:fs/promises";import i from"node:path";import{systemModulesTable as a}from"@arch-cadre/core";import{eq as o}from"drizzle-orm";const s=globalThis;s.__KRYO_REGISTERED_MODULES__||=new Map;async function c(e){let t=s.__KRYO_REGISTERED_MODULES__;for(let n of e)n.manifest?.id&&t.set(n.manifest.id,n)}async function l(){try{let o=await n(),c=[],l=await r.readdir(o).catch(()=>[]);for(let t of l)if(!t.startsWith(`.`))try{let n=await r.readFile(i.join(o,t,`manifeston`),`utf-8`),a=e.parse(JSON.parse(n));c.push({...a,hasDocs:!1})}catch{}let u=s.__KRYO_REGISTERED_MODULES__;for(let e of u.values())c.some(t=>t.id===e.manifest.id)||c.push({...e.manifest,hasDocs:!1});let d=await t.select().from(a).catch(()=>[]),f=new Map(d.map(e=>[e.id,e]));return c.map(e=>{let t=f.get(e.id);return{...e,enabled:e.system?!0:t?.enabled??!1,installed:e.system?!0:t?.installed??!1,lastStep:t?.lastStep,hasDocs:e.hasDocs}})}catch{return[]}}async function u(e){try{let[n]=await t.select().from(a).where(o(a.id,e));return{enabled:n?.enabled,installed:n?.installed,lastStep:n?.lastStep}}catch{return{enabled:!1,installed:!1,lastStep:null}}}async function d(e){try{let[n]=await t.select().from(a).where(o(a.id,e));return!!n?.enabled||!!n?.system}catch{return!1}}async function f(e){try{let t=s.__KRYO_REGISTERED_MODULES__;if(t.has(e))return console.log(`[Kernel:Manage] Module "${e}" found in registry.`),t.get(e)||null;if(console.log(`[Kernel:Manage] Module "${e}" not in registry. Registry size: ${t.size}. Keys: ${Array.from(t.keys()).join(`, `)}`),!e.startsWith(`.`)&&!e.startsWith(`/`))try{let t=await import(`@arch-cadre/${e}`);return console.log(`[Kernel:Manage] Module "${e}" loaded via import().`),t.default||t||null}catch(t){console.warn(`[Kernel:Manage] Failed to import module "${e} via import().": ${t.message}`)}return null}catch(t){return console.error(`[Kernel:Manage] Error in getModuleInstance for "${e}":`,t),null}}async function p(e){try{let[n]=await t.select({config:a.config}).from(a).where(o(a.id,e));return n?.config?JSON.parse(n.config):null}catch{return null}}async function m(e,n){try{await t.update(a).set({config:JSON.stringify(n)}).where(o(a.id,e))}catch(t){console.warn(`[Kernel:Manage] Failed to update config for ${e}:`,t)}}export{p as getModuleConfig,f as getModuleInstance,u as getModuleStatus,l as getModules,d as isModuleEnabled,c as registerModules,m as updateModuleConfig};
|
package/dist/server/registry.cjs
CHANGED
|
@@ -1,94 +1 @@
|
|
|
1
|
-
"use
|
|
2
|
-
"use server";
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.initModules = initModules;
|
|
8
|
-
var _core = require("@arch-cadre/core");
|
|
9
|
-
var _server = require("@arch-cadre/core/server");
|
|
10
|
-
var _drizzleOrm = require("drizzle-orm");
|
|
11
|
-
var _manage = require("./manage.cjs");
|
|
12
|
-
const globalForRegistry = globalThis;
|
|
13
|
-
async function initModules(force = false) {
|
|
14
|
-
if (globalForRegistry.__KRYO_MODULES_INITIALIZED__ && !force) return;
|
|
15
|
-
if (force) {
|
|
16
|
-
console.log("[Kernel:Registry] Forcing re-initialization...");
|
|
17
|
-
_core.eventBus.clearAll();
|
|
18
|
-
}
|
|
19
|
-
console.log("[Kernel:Registry] Synchronizing module listeners...");
|
|
20
|
-
globalForRegistry.__KRYO_MODULES_INITIALIZED__ = true;
|
|
21
|
-
await _core.eventBus.publish("system:modules:init:start", {
|
|
22
|
-
timestamp: Date.now()
|
|
23
|
-
});
|
|
24
|
-
const processedModuleIds = /* @__PURE__ */new Set();
|
|
25
|
-
let hasNewModules = true;
|
|
26
|
-
let iterations = 0;
|
|
27
|
-
const MAX_ITERATIONS = 10;
|
|
28
|
-
while (hasNewModules && iterations < MAX_ITERATIONS) {
|
|
29
|
-
hasNewModules = false;
|
|
30
|
-
iterations++;
|
|
31
|
-
const currentModules = await (0, _manage.getModules)();
|
|
32
|
-
const pendingModules = currentModules.filter(mod => !processedModuleIds.has(mod.id));
|
|
33
|
-
if (pendingModules.length === 0) {
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
for (const mod of pendingModules) {
|
|
37
|
-
try {
|
|
38
|
-
await _server.db.insert(_core.systemModulesTable).values({
|
|
39
|
-
id: mod.id,
|
|
40
|
-
enabled: mod.system ?? false,
|
|
41
|
-
installed: mod.system ?? false,
|
|
42
|
-
system: mod.system ?? false
|
|
43
|
-
}).onConflictDoNothing();
|
|
44
|
-
} catch (_e) {}
|
|
45
|
-
}
|
|
46
|
-
const sortedPending = [...pendingModules].sort((a, b) => a.system === b.system ? 0 : a.system ? -1 : 1);
|
|
47
|
-
for (const mod of sortedPending) {
|
|
48
|
-
processedModuleIds.add(mod.id);
|
|
49
|
-
hasNewModules = true;
|
|
50
|
-
try {
|
|
51
|
-
const enabled = await (0, _manage.isModuleEnabled)(mod.id);
|
|
52
|
-
if (!enabled) continue;
|
|
53
|
-
const instance = await (0, _manage.getModuleInstance)(mod.id);
|
|
54
|
-
if (!instance) {
|
|
55
|
-
console.warn(`[Kernel:Registry] No instance found for module ${mod.id}. Ensure it is registered correctly.`);
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
let dbMod = null;
|
|
59
|
-
try {
|
|
60
|
-
[dbMod] = await _server.db.select().from(_core.systemModulesTable).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, mod.id));
|
|
61
|
-
} catch (_e) {}
|
|
62
|
-
if (enabled && (!dbMod || !dbMod.installed)) {
|
|
63
|
-
console.log(`[Kernel:Registry] Installing module ${mod.id}...`);
|
|
64
|
-
if (instance.onMigrate) {
|
|
65
|
-
console.log(`[Kernel:Registry] Running onMigrate for ${mod.id}...`);
|
|
66
|
-
await instance.onMigrate();
|
|
67
|
-
}
|
|
68
|
-
console.log(`[Kernel:Registry] Running onEnable for ${mod.id}...`);
|
|
69
|
-
if (instance.onEnable) await instance.onEnable();
|
|
70
|
-
try {
|
|
71
|
-
await _server.db.update(_core.systemModulesTable).set({
|
|
72
|
-
installed: true,
|
|
73
|
-
lastStep: null
|
|
74
|
-
}).where((0, _drizzleOrm.eq)(_core.systemModulesTable.id, mod.id));
|
|
75
|
-
} catch (_e) {}
|
|
76
|
-
}
|
|
77
|
-
if (instance.init) {
|
|
78
|
-
console.log(`[Kernel:Registry] Initializing ${mod.id}...`);
|
|
79
|
-
await instance.init();
|
|
80
|
-
console.log(`[Kernel:Registry] Module ${mod.id} is active.`);
|
|
81
|
-
}
|
|
82
|
-
} catch (error) {
|
|
83
|
-
console.error(`[Kernel:Registry] Critical failure for ${mod.id}:`, error);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (iterations >= MAX_ITERATIONS) {
|
|
88
|
-
console.warn("[Kernel:Registry] Max init iterations reached. Check for circular module registrations.");
|
|
89
|
-
}
|
|
90
|
-
await _core.eventBus.publish("system:modules:init:end", {
|
|
91
|
-
timestamp: Date.now(),
|
|
92
|
-
moduleCount: processedModuleIds.size
|
|
93
|
-
});
|
|
94
|
-
}
|
|
1
|
+
"use server";require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`./manage.cjs`);let t=require(`@arch-cadre/core/server`),n=require(`@arch-cadre/core`),r=require(`drizzle-orm`);const i=globalThis;async function a(a=!1){if(i.__KRYO_MODULES_INITIALIZED__&&!a)return;a&&(console.log(`[Kernel:Registry] Forcing re-initialization...`),n.eventBus.clearAll()),console.log(`[Kernel:Registry] Synchronizing module listeners...`),i.__KRYO_MODULES_INITIALIZED__=!0,await n.eventBus.publish(`system:modules:init:start`,{timestamp:Date.now()});let o=new Set,s=!0,c=0;for(;s&&c<10;){s=!1,c++;let i=(await e.getModules()).filter(e=>!o.has(e.id));if(i.length===0)break;for(let e of i)try{await t.db.insert(n.systemModulesTable).values({id:e.id,enabled:e.system??!1,installed:e.system??!1,system:e.system??!1}).onConflictDoNothing()}catch{}let a=[...i].sort((e,t)=>e.system===t.system?0:e.system?-1:1);for(let i of a){o.add(i.id),s=!0;try{let a=await e.isModuleEnabled(i.id);if(!a)continue;let o=await e.getModuleInstance(i.id);if(!o){console.warn(`[Kernel:Registry] No instance found for module ${i.id}. Ensure it is registered correctly.`);continue}let s=null;try{[s]=await t.db.select().from(n.systemModulesTable).where((0,r.eq)(n.systemModulesTable.id,i.id))}catch{}if(a&&(!s||!s.installed)){console.log(`[Kernel:Registry] Installing module ${i.id}...`),o.onMigrate&&(console.log(`[Kernel:Registry] Running onMigrate for ${i.id}...`),await o.onMigrate()),console.log(`[Kernel:Registry] Running onEnable for ${i.id}...`),o.onEnable&&await o.onEnable();try{await t.db.update(n.systemModulesTable).set({installed:!0,lastStep:null}).where((0,r.eq)(n.systemModulesTable.id,i.id))}catch{}}o.init&&(console.log(`[Kernel:Registry] Initializing ${i.id}...`),await o.init(),console.log(`[Kernel:Registry] Module ${i.id} is active.`))}catch(e){console.error(`[Kernel:Registry] Critical failure for ${i.id}:`,e)}}}c>=10&&console.warn(`[Kernel:Registry] Max init iterations reached. Check for circular module registrations.`),await n.eventBus.publish(`system:modules:init:end`,{timestamp:Date.now(),moduleCount:o.size})}exports.initModules=a;
|