@mebular/mcp 0.1.2 → 0.2.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/src/serve.mjs CHANGED
@@ -20,6 +20,7 @@ import { READ_ROUTES, buildHandoffPlan } from './admin.mjs';
20
20
  import { configPath } from './config.mjs';
21
21
  import { createServiceFacade } from './service-facade.mjs';
22
22
  import { APP_VERSION } from './version.mjs';
23
+ import { resolveStaticAssetRoots } from './asset-roots.mjs';
23
24
 
24
25
  // 兼容既有导入方:APP_VERSION 的真源是 ./version.mjs。
25
26
  export { APP_VERSION } from './version.mjs';
@@ -229,12 +230,11 @@ export const EMBED_PROTOCOL = 1;
229
230
 
230
231
  // ---------- 控制台静态托管(D1) ----------
231
232
  // 路径白名单:只认识这几个确切文件名,天然防目录穿越。允许 MEBULAR_CONSOLE_DIR 覆盖。
232
- const CONSOLE_DIR =
233
- process.env.MEBULAR_CONSOLE_DIR ?? join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'console');
233
+ const DEFAULT_ASSET_ROOTS = resolveStaticAssetRoots(import.meta.url);
234
+ const CONSOLE_DIR = process.env.MEBULAR_CONSOLE_DIR ?? DEFAULT_ASSET_ROOTS.consoleDir;
234
235
  // locale pack 数据层:唯一注册表 + 每语言目录(只读 JSON)。
235
236
  // 允许 MEBULAR_LOCALES_DIR 覆盖(桌面打包时 extraResources 落在 <resources>/locales)。
236
- const LOCALES_DIR =
237
- process.env.MEBULAR_LOCALES_DIR ?? join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'locales');
237
+ const LOCALES_DIR = process.env.MEBULAR_LOCALES_DIR ?? DEFAULT_ASSET_ROOTS.localesDir;
238
238
  const LOCALE_CODE_RE = /^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/;
239
239
  const LOCALE_PACK_FILES = new Set(['console.json', 'desktop.json', 'errors.json']);
240
240
  const CONSOLE_FILES = {
@@ -1789,12 +1789,18 @@ export async function startHttpServer({
1789
1789
  }
1790
1790
  let plan;
1791
1791
  try {
1792
- const { restartPlanFor } = await import('../../service/dist/index.js');
1793
- plan = restartPlanFor({ kind, args: [], heartbeatDir: home, env: { MEBULAR_HOME: home } }, { home });
1792
+ const { planRuntimeServiceRestart, runtimeProviderFromEnv } = await import('./runtime-service.mjs');
1793
+ const { resolveServiceHome } = await import('./service-control.mjs');
1794
+ plan = await planRuntimeServiceRestart({
1795
+ root: cloud.available ? cloud.root : null,
1796
+ home,
1797
+ serviceHome: resolveServiceHome(),
1798
+ fallbackProvider: runtimeProviderFromEnv({ cliPath: process.argv[1] }),
1799
+ });
1794
1800
  } catch (error) {
1795
1801
  return { registered: false, manual: `服务管理不可用(${String(error?.message ?? error)}):请手动重启` };
1796
1802
  }
1797
- const command = plan.commands.map((entry) => entry.join(' ')).join(' && ') || null;
1803
+ const command = plan.commands?.map((entry) => entry.join(' ')).join(' && ') || null;
1798
1804
  if (!plan.registered) return { registered: false, command, manual: plan.manual };
1799
1805
  const dryRun = process.env.MEBULAR_RESTART_DRY_RUN === '1';
1800
1806
  return {
@@ -2753,21 +2759,63 @@ export async function startHttpServer({
2753
2759
  return sendJson(res, result.ok ? 200 : 409, result);
2754
2760
  }
2755
2761
 
2756
- // H-1:常驻运行(服务托管,动作式;服务命令带**守护自身 home**)。CI 用 MEBULAR_SERVICE_DRY_RUN=1 替身断言命令序列。
2762
+ // H-1:常驻运行(服务托管,动作式;统一走共享 runtime/service 层)。CI 用 MEBULAR_SERVICE_DRY_RUN=1 替身断言命令序列。
2757
2763
  const svcAction = path.match(/^\/admin\/api\/service\/(install|uninstall)$/);
2758
2764
  if (svcAction) {
2759
- const { installServiceForHome, uninstallServiceForHome, serviceStatusForHome, resolveServiceHome } = await import('./service-control.mjs');
2765
+ const {
2766
+ installRuntimeService,
2767
+ runtimeProviderFromEnv,
2768
+ runtimeServiceStatus,
2769
+ uninstallRuntimeService,
2770
+ } = await import('./runtime-service.mjs');
2771
+ const { resolveServiceHome } = await import('./service-control.mjs');
2760
2772
  const dryRun = process.env.MEBULAR_SERVICE_DRY_RUN === '1';
2761
2773
  const commands = [];
2762
2774
  const run = dryRun ? (cmd, args) => { commands.push([cmd, ...(args ?? [])]); return { status: 0, stdout: '', stderr: '' }; } : undefined;
2763
2775
  // 两套 home:home = 实例身份(descriptor),serviceHome = unit/manifest 落盘(用户 OS home)。
2764
2776
  const serviceHome = resolveServiceHome();
2777
+ const root = cloud.available ? cloud.root : null;
2778
+ const fallbackProvider = runtimeProviderFromEnv({ cliPath: process.argv[1] });
2779
+ if (root === null) {
2780
+ // 自定义 home / 受限 env:保留 App 既有 service-control 行为。
2781
+ const { installServiceForHome, uninstallServiceForHome, serviceStatusForHome } = await import('./service-control.mjs');
2782
+ try {
2783
+ const action = svcAction[1];
2784
+ const result = action === 'install'
2785
+ ? installServiceForHome({ home, serviceHome, ...(run ? { run } : {}) })
2786
+ : uninstallServiceForHome({ home, serviceHome, ...(run ? { run } : {}) });
2787
+ const status = serviceStatusForHome({ home, serviceHome, ...(run ? { run } : {}) });
2788
+ return sendJson(res, 200, { ok: true, action, dryRun, commands, result, status });
2789
+ } catch (error) {
2790
+ return sendJson(res, 500, { ok: false, action: svcAction[1], error: 'service_action_failed', message: String(error?.message ?? error) });
2791
+ }
2792
+ }
2765
2793
  try {
2766
2794
  const action = svcAction[1];
2767
2795
  const result = action === 'install'
2768
- ? installServiceForHome({ home, serviceHome, ...(run ? { run } : {}) })
2769
- : uninstallServiceForHome({ home, serviceHome, ...(run ? { run } : {}) });
2770
- const status = serviceStatusForHome({ home, serviceHome, ...(run ? { run } : {}) });
2796
+ ? await installRuntimeService({ root, home, serviceHome, http: { port }, fallbackProvider, ...(run ? { run } : {}) })
2797
+ : await uninstallRuntimeService({ root, home, serviceHome, ...(run ? { run } : {}) });
2798
+ const status = await runtimeServiceStatus({
2799
+ root,
2800
+ home,
2801
+ serviceHome,
2802
+ fallbackProvider,
2803
+ ...(run ? { run } : {}),
2804
+ });
2805
+ if (result?.ok !== true) {
2806
+ const httpStatus = ['runtime_required', 'service_conflict', 'service_foreign'].includes(result?.reason) ? 409 : 500;
2807
+ return sendJson(res, httpStatus, {
2808
+ ok: false,
2809
+ action,
2810
+ dryRun,
2811
+ commands,
2812
+ reason: result?.reason ?? 'service_action_failed',
2813
+ message: result?.message ?? 'service action failed',
2814
+ current: result?.current ?? null,
2815
+ result,
2816
+ status,
2817
+ });
2818
+ }
2771
2819
  return sendJson(res, 200, { ok: true, action, dryRun, commands, result, status });
2772
2820
  } catch (error) {
2773
2821
  return sendJson(res, 500, { ok: false, action: svcAction[1], error: 'service_action_failed', message: String(error?.message ?? error) });
@@ -39,14 +39,33 @@ export function cliBinPath() {
39
39
  }
40
40
 
41
41
  /** 守护服务描述符:**必须**携带守护自身 home(env + workingDir + heartbeatDir)。 */
42
- export function serviceDescriptor({ home, execPath = process.execPath, extraArgs = [] } = {}) {
42
+ export function serviceDescriptor({ home, execPath = process.execPath, extraArgs = [], cloudRoot = null } = {}) {
43
+ const cliPath = cliBinPath();
43
44
  return {
44
45
  kind: SERVICE_KIND,
45
- args: [cliBinPath(), 'serve', ...extraArgs],
46
+ args: [cliPath, 'serve', ...extraArgs],
46
47
  heartbeatDir: home,
47
48
  workingDir: home,
48
49
  execPath,
49
- env: { MEBULAR_HOME: home },
50
+ env: {
51
+ MEBULAR_HOME: home,
52
+ ...(typeof cloudRoot === 'string' && cloudRoot.length > 0 ? { MEBULAR_CLOUD_ROOT: cloudRoot } : {}),
53
+ MEBULAR_RUNTIME_PROVIDER: 'cli',
54
+ MEBULAR_RUNTIME_OWNER: 'cli',
55
+ MEBULAR_RUNTIME_PATH: cliPath,
56
+ ELECTRON_RUN_AS_NODE: '1',
57
+ },
58
+ runtime: {
59
+ version: null,
60
+ runtimePath: null,
61
+ cliPath,
62
+ provider: 'cli',
63
+ owner: 'cli',
64
+ execPath,
65
+ electron: Boolean(process.versions.electron),
66
+ cloudRoot: typeof cloudRoot === 'string' && cloudRoot.length > 0 ? cloudRoot : null,
67
+ home,
68
+ },
50
69
  };
51
70
  }
52
71
 
@@ -0,0 +1,84 @@
1
+ // 共享 service 加载器:bare `@mebular/service` 优先,安装布局下回退到 core tarball 内的
2
+ // `packages/service/dist/index.js`。
3
+ //
4
+ // 背景(Phase 4a blocker):
5
+ // `@mebular/core` 的 package.json 不依赖 `@mebular/service`,但 core tarball 实际包含
6
+ // `packages/service/dist`。共享 runtime 安装到 `<cloudRoot>/runtime/<version>/node_modules/
7
+ // @mebular/core/...` 后,bare import 解析不到独立包;若不回退,`runtime resolve` 的
8
+ // service 状态会变成 `service_unknown`,plugin-only 首次启动会被阻断。
9
+ //
10
+ // 决策:不新增 npm 依赖、不改发布依赖;由本 loader 统一 bare → bundled dist。
11
+ // App 仍可注入自己的 service adapter(cloud-switch 的 `deps` / supervisor 的注入点),
12
+ // 本 loader 只服务“默认共享 core/CLI”路径。
13
+ //
14
+ // 约束:
15
+ // - 只接受 bare 包名或本文件相对定位的 bundled dist,不加载任意 URL/路径;
16
+ // - 只缓存成功结果;失败不缓存,便于用户修复安装后重试;
17
+ // - 失败抛 `SERVICE_MODULE_UNAVAILABLE`,保留 bare/bundled 两个 cause 供诊断(不含密钥)。
18
+
19
+ const BUNDLED_SERVICE_URL = new URL('../../service/dist/index.js', import.meta.url);
20
+
21
+ const importBareService = () => import('@mebular/service');
22
+ const importBundledService = () => import(BUNDLED_SERVICE_URL.href);
23
+
24
+ let cachedModule = null;
25
+ let cachedPromise = null;
26
+
27
+ /** 本 loader 在安装布局中定位到的 service dist(测试/诊断用;不返回文件内容)。 */
28
+ export function bundledServiceModuleUrl() {
29
+ return BUNDLED_SERVICE_URL;
30
+ }
31
+
32
+ /** 清空缓存(测试或修复安装后显式重试)。 */
33
+ export function resetServiceModuleCache() {
34
+ cachedModule = null;
35
+ cachedPromise = null;
36
+ }
37
+
38
+ /**
39
+ * 加载 service 模块。
40
+ *
41
+ * `importBare` / `importBundled` 可注入,便于离线测试安装布局;生产调用无需传参。
42
+ * 返回 ES module namespace,与 `import('@mebular/service')` 形状一致。
43
+ */
44
+ export async function loadServiceModule({
45
+ importBare = importBareService,
46
+ importBundled = importBundledService,
47
+ cache = true,
48
+ } = {}) {
49
+ if (cache && cachedModule !== null) return cachedModule;
50
+ if (cache && cachedPromise !== null) return cachedPromise;
51
+
52
+ const load = async () => {
53
+ try {
54
+ return await importBare();
55
+ } catch (bareError) {
56
+ try {
57
+ return await importBundled();
58
+ } catch (bundledError) {
59
+ const error = new Error(
60
+ '无法加载 @mebular/service:bare 包与 core 内置 packages/service/dist 均不可用',
61
+ );
62
+ error.code = 'SERVICE_MODULE_UNAVAILABLE';
63
+ error.cause = bareError;
64
+ error.bareError = bareError;
65
+ error.bundledError = bundledError;
66
+ throw error;
67
+ }
68
+ }
69
+ };
70
+
71
+ const pending = load();
72
+ if (cache) cachedPromise = pending;
73
+ try {
74
+ const module = await pending;
75
+ if (cache) {
76
+ cachedModule = module;
77
+ cachedPromise = null;
78
+ }
79
+ return module;
80
+ } catch (error) {
81
+ if (cache) cachedPromise = null;
82
+ throw error;
83
+ }
84
+ }
package/src/version.mjs CHANGED
@@ -5,4 +5,4 @@
5
5
  // scripts/bump-version.mjs 只改这一处,scripts/verify-release-version.mjs
6
6
  // 校验它等于发布版本;不要在其它文件再写版本字面量。
7
7
 
8
- export const APP_VERSION = '0.1.2';
8
+ export const APP_VERSION = '0.2.1';