@chatbi-v/core 2.1.3 → 2.1.5

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/index.d.mts CHANGED
@@ -1209,11 +1209,18 @@ declare class PluginManager {
1209
1209
  * @param notify - 是否在注册完成后触发状态变更通知
1210
1210
  */
1211
1211
  register(plugin: Plugin, notify?: boolean): void;
1212
+ /**
1213
+ * 手动挂载指定插件(支持按需加载)
1214
+ * @description 如果插件依赖尚未挂载,会递归挂载所有依赖
1215
+ * @param pluginId 插件 ID
1216
+ */
1217
+ mountPlugin(pluginId: string): Promise<void>;
1212
1218
  /**
1213
1219
  * 初始化所有插件
1214
1220
  * @param sharedContext 共享上下文
1221
+ * @param autoMount 是否自动挂载所有插件 (默认为 false,启用按需加载模式)
1215
1222
  */
1216
- initPlugins(sharedContext?: Record<string, any>): Promise<void>;
1223
+ initPlugins(sharedContext?: Record<string, any>, autoMount?: boolean): Promise<void>;
1217
1224
  /**
1218
1225
  * 获取排序后的插件 ID 列表 (处理依赖)
1219
1226
  */
@@ -1222,8 +1229,9 @@ declare class PluginManager {
1222
1229
  * 加载插件列表
1223
1230
  * @param configs 插件配置
1224
1231
  * @param registry 插件注册表 (动态导入函数)
1232
+ * @param notify 是否在加载完成后触发通知,默认为 true
1225
1233
  */
1226
- loadPlugins(configs: Record<string, any>, registry: Record<string, () => Promise<any>>): Promise<void>;
1234
+ loadPlugins(configs: Record<string, any>, registry: Record<string, () => Promise<any>>, notify?: boolean): Promise<void>;
1227
1235
  /**
1228
1236
  * 加载远程插件
1229
1237
  * @param pluginId 插件 ID
package/dist/index.d.ts CHANGED
@@ -1209,11 +1209,18 @@ declare class PluginManager {
1209
1209
  * @param notify - 是否在注册完成后触发状态变更通知
1210
1210
  */
1211
1211
  register(plugin: Plugin, notify?: boolean): void;
1212
+ /**
1213
+ * 手动挂载指定插件(支持按需加载)
1214
+ * @description 如果插件依赖尚未挂载,会递归挂载所有依赖
1215
+ * @param pluginId 插件 ID
1216
+ */
1217
+ mountPlugin(pluginId: string): Promise<void>;
1212
1218
  /**
1213
1219
  * 初始化所有插件
1214
1220
  * @param sharedContext 共享上下文
1221
+ * @param autoMount 是否自动挂载所有插件 (默认为 false,启用按需加载模式)
1215
1222
  */
1216
- initPlugins(sharedContext?: Record<string, any>): Promise<void>;
1223
+ initPlugins(sharedContext?: Record<string, any>, autoMount?: boolean): Promise<void>;
1217
1224
  /**
1218
1225
  * 获取排序后的插件 ID 列表 (处理依赖)
1219
1226
  */
@@ -1222,8 +1229,9 @@ declare class PluginManager {
1222
1229
  * 加载插件列表
1223
1230
  * @param configs 插件配置
1224
1231
  * @param registry 插件注册表 (动态导入函数)
1232
+ * @param notify 是否在加载完成后触发通知,默认为 true
1225
1233
  */
1226
- loadPlugins(configs: Record<string, any>, registry: Record<string, () => Promise<any>>): Promise<void>;
1234
+ loadPlugins(configs: Record<string, any>, registry: Record<string, () => Promise<any>>, notify?: boolean): Promise<void>;
1227
1235
  /**
1228
1236
  * 加载远程插件
1229
1237
  * @param pluginId 插件 ID
package/dist/index.js CHANGED
@@ -1781,11 +1781,53 @@ var PluginManager = class {
1781
1781
  this.notify();
1782
1782
  }
1783
1783
  }
1784
+ /**
1785
+ * 手动挂载指定插件(支持按需加载)
1786
+ * @description 如果插件依赖尚未挂载,会递归挂载所有依赖
1787
+ * @param pluginId 插件 ID
1788
+ */
1789
+ async mountPlugin(pluginId) {
1790
+ const runtime = this.runtimes.get(pluginId);
1791
+ if (!runtime) {
1792
+ const msg = `\u5C1D\u8BD5\u6302\u8F7D\u4E0D\u5B58\u5728\u6216\u672A\u521D\u59CB\u5316\u7684\u63D2\u4EF6: ${pluginId}`;
1793
+ logger6.warn(msg);
1794
+ throw new Error(msg);
1795
+ }
1796
+ if (runtime.status === "mounted") return;
1797
+ const plugin = this.plugins.get(pluginId);
1798
+ if (plugin?.metadata.dependencies) {
1799
+ for (const depId of plugin.metadata.dependencies) {
1800
+ if (!this.runtimes.has(depId)) {
1801
+ logger6.error(`\u63D2\u4EF6 ${pluginId} \u4F9D\u8D56\u7F3A\u5931: ${depId}`);
1802
+ throw new Error(`Dependency ${depId} missing for ${pluginId}`);
1803
+ }
1804
+ await this.mountPlugin(depId);
1805
+ }
1806
+ }
1807
+ if (runtime.status === "initial") {
1808
+ try {
1809
+ await runtime.load();
1810
+ } catch (e) {
1811
+ logger6.error(`\u63D2\u4EF6 ${pluginId} load \u5931\u8D25:`, e);
1812
+ throw e;
1813
+ }
1814
+ }
1815
+ if (runtime.status === "loaded") {
1816
+ try {
1817
+ await runtime.mount();
1818
+ this.notify();
1819
+ } catch (e) {
1820
+ logger6.error(`\u63D2\u4EF6 ${pluginId} \u6302\u8F7D\u5931\u8D25:`, e);
1821
+ throw e;
1822
+ }
1823
+ }
1824
+ }
1784
1825
  /**
1785
1826
  * 初始化所有插件
1786
1827
  * @param sharedContext 共享上下文
1828
+ * @param autoMount 是否自动挂载所有插件 (默认为 false,启用按需加载模式)
1787
1829
  */
1788
- async initPlugins(sharedContext = {}) {
1830
+ async initPlugins(sharedContext = {}, autoMount = false) {
1789
1831
  if (this.isInitializing) {
1790
1832
  logger6.warn("PluginManager is already initializing, skipping...");
1791
1833
  return;
@@ -1837,6 +1879,9 @@ var PluginManager = class {
1837
1879
  }
1838
1880
  for (const id of sortedPluginIds) {
1839
1881
  if (!idsToLoad.has(id)) continue;
1882
+ const plugin = this.plugins.get(id);
1883
+ const shouldMount = autoMount || plugin && ["system", "theme"].includes(plugin.metadata.type);
1884
+ if (!shouldMount) continue;
1840
1885
  const runtime = this.runtimes.get(id);
1841
1886
  if (runtime && (runtime.status === "loaded" || runtime.status === "initial")) {
1842
1887
  try {
@@ -1848,6 +1893,7 @@ var PluginManager = class {
1848
1893
  }
1849
1894
  } finally {
1850
1895
  this.isInitializing = false;
1896
+ this.notify();
1851
1897
  }
1852
1898
  }
1853
1899
  /**
@@ -1894,8 +1940,9 @@ var PluginManager = class {
1894
1940
  * 加载插件列表
1895
1941
  * @param configs 插件配置
1896
1942
  * @param registry 插件注册表 (动态导入函数)
1943
+ * @param notify 是否在加载完成后触发通知,默认为 true
1897
1944
  */
1898
- async loadPlugins(configs, registry) {
1945
+ async loadPlugins(configs, registry, notify = true) {
1899
1946
  logger6.info("\u5F00\u59CB\u52A0\u8F7D\u63D2\u4EF6...");
1900
1947
  const localLoadPromises = Object.entries(registry).map(async ([registryId, importFn]) => {
1901
1948
  try {
@@ -1929,7 +1976,9 @@ var PluginManager = class {
1929
1976
  this.register(plugin, false);
1930
1977
  }
1931
1978
  });
1932
- this.notify();
1979
+ if (notify) {
1980
+ this.notify();
1981
+ }
1933
1982
  logger6.info(`\u63D2\u4EF6\u52A0\u8F7D\u5B8C\u6210\uFF0C\u5171\u52A0\u8F7D ${this.plugins.size} \u4E2A\u63D2\u4EF6`);
1934
1983
  }
1935
1984
  /**
@@ -2119,6 +2168,17 @@ var SlotSkeletons = ({ slot, expanded }) => {
2119
2168
 
2120
2169
  // src/components/PluginSlot.tsx
2121
2170
  var import_jsx_runtime4 = require("react/jsx-runtime");
2171
+ var ExtensionLoader = ({ pluginId, component: Component2, props }) => {
2172
+ const status = pluginManager.getPluginRuntimeStatus(pluginId);
2173
+ if (status !== "mounted") {
2174
+ const error = pluginManager.getPluginError(pluginId);
2175
+ if (error) {
2176
+ throw error;
2177
+ }
2178
+ throw pluginManager.mountPlugin(pluginId);
2179
+ }
2180
+ return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Component2, { ...props });
2181
+ };
2122
2182
  var PluginSlot = ({
2123
2183
  slot,
2124
2184
  props = {},
@@ -2131,7 +2191,9 @@ var PluginSlot = ({
2131
2191
  const [, forceUpdate] = (0, import_react4.useState)({});
2132
2192
  (0, import_react4.useEffect)(() => {
2133
2193
  const unsubscribe = pluginManager.subscribe(() => {
2134
- forceUpdate({});
2194
+ (0, import_react4.startTransition)(() => {
2195
+ forceUpdate({});
2196
+ });
2135
2197
  }, slot);
2136
2198
  return unsubscribe;
2137
2199
  }, [slot]);
@@ -2152,7 +2214,14 @@ var PluginSlot = ({
2152
2214
  return {
2153
2215
  key,
2154
2216
  extension: ext,
2155
- component: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PluginErrorBoundary, { pluginId: ext._pluginId, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react4.Suspense, { fallback: skeleton || /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(SlotSkeletons, { slot }), children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(Component2, { ...mergedProps }) }) }, key)
2217
+ component: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(PluginErrorBoundary, { pluginId: ext._pluginId, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react4.Suspense, { fallback: skeleton || /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(SlotSkeletons, { slot }), children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
2218
+ ExtensionLoader,
2219
+ {
2220
+ pluginId: ext._pluginId || "",
2221
+ component: Component2,
2222
+ props: mergedProps
2223
+ }
2224
+ ) }) }, key)
2156
2225
  };
2157
2226
  });
2158
2227
  }, [extensions, mergedProps]);
@@ -2774,10 +2843,10 @@ var usePluginLoader = (options) => {
2774
2843
  const { configManager: configManager2 } = await Promise.resolve().then(() => (init_config_manager(), config_manager_exports));
2775
2844
  configManager2.set("system", options.systemConfig);
2776
2845
  }
2777
- await pluginManager.loadPlugins(pluginConfigs, finalRegistry);
2778
- await pluginManager.initPlugins(sharedContext);
2846
+ await pluginManager.loadPlugins(pluginConfigs, finalRegistry, false);
2847
+ await pluginManager.initPlugins(sharedContext, false);
2779
2848
  setPluginsLoaded(true);
2780
- logger10.info("Plugins loaded successfully");
2849
+ logger10.info("Plugins loaded successfully (Lazy Mode)");
2781
2850
  } catch (error) {
2782
2851
  logger10.error("Failed to load plugins:", error);
2783
2852
  } finally {
@@ -2785,14 +2854,9 @@ var usePluginLoader = (options) => {
2785
2854
  }
2786
2855
  };
2787
2856
  load();
2788
- return () => {
2789
- unsubscribe();
2790
- };
2857
+ return unsubscribe;
2791
2858
  }, []);
2792
- return {
2793
- pluginsLoaded,
2794
- pluginVersion
2795
- };
2859
+ return { pluginsLoaded, pluginVersion };
2796
2860
  };
2797
2861
  // Annotate the CommonJS export names for ESM import in node:
2798
2862
  0 && (module.exports = {