@chatbi-v/core 2.1.2 → 2.1.4

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.mjs CHANGED
@@ -1240,6 +1240,8 @@ var PluginManager = class {
1240
1240
  sharedContext = null;
1241
1241
  /** 收集到的插件工具函数集合 */
1242
1242
  utils = {};
1243
+ /** 是否正在初始化插件中,防止重入导致多次加载 */
1244
+ isInitializing = false;
1243
1245
  /**
1244
1246
  * 构造函数
1245
1247
  * @param storage - 底层存储适配器
@@ -1642,58 +1644,69 @@ var PluginManager = class {
1642
1644
  * @param sharedContext 共享上下文
1643
1645
  */
1644
1646
  async initPlugins(sharedContext = {}) {
1645
- this.sharedContext = {
1646
- ...sharedContext,
1647
- events: this.eventBus
1648
- };
1649
- this.plugins.forEach((plugin) => {
1650
- if (!this.isPluginEnabled(plugin.id)) return;
1651
- if (!this.runtimes.has(plugin.id)) {
1652
- const runtime = new PluginRuntime(plugin, this.sharedContext, this.storageManager);
1653
- this.runtimes.set(plugin.id, runtime);
1654
- }
1655
- });
1656
- const sortedPluginIds = this.getSortedPluginIds();
1657
- const missingDeps = /* @__PURE__ */ new Map();
1658
- const idsToLoad = new Set(sortedPluginIds);
1659
- for (const id of sortedPluginIds) {
1660
- const plugin = this.plugins.get(id);
1661
- if (plugin?.metadata.dependencies) {
1662
- const missing = plugin.metadata.dependencies.filter((depId) => !this.runtimes.has(depId));
1663
- if (missing.length > 0) {
1664
- missingDeps.set(id, missing);
1665
- idsToLoad.delete(id);
1666
- this.runtimes.delete(id);
1667
- }
1668
- }
1647
+ if (this.isInitializing) {
1648
+ logger6.warn("PluginManager is already initializing, skipping...");
1649
+ return;
1669
1650
  }
1670
- if (missingDeps.size > 0) {
1671
- missingDeps.forEach((deps, id) => {
1672
- logger6.error(`\u63D2\u4EF6 ${id} \u65E0\u6CD5\u52A0\u8F7D\uFF0C\u7F3A\u5931\u4F9D\u8D56: ${deps.join(", ")}`);
1651
+ this.isInitializing = true;
1652
+ try {
1653
+ this.sharedContext = {
1654
+ ...sharedContext,
1655
+ events: this.eventBus
1656
+ };
1657
+ this.plugins.forEach((plugin) => {
1658
+ if (!this.isPluginEnabled(plugin.id)) return;
1659
+ if (!this.runtimes.has(plugin.id)) {
1660
+ const runtime = new PluginRuntime(plugin, this.sharedContext, this.storageManager);
1661
+ this.runtimes.set(plugin.id, runtime);
1662
+ }
1673
1663
  });
1674
- }
1675
- for (const id of sortedPluginIds) {
1676
- if (!idsToLoad.has(id)) continue;
1677
- const runtime = this.runtimes.get(id);
1678
- if (runtime) {
1679
- try {
1680
- logger6.info(`[PluginManager] invoking onLoad for ${id}`);
1681
- await runtime.load();
1682
- logger6.info(`[PluginManager] onLoad completed for ${id}`);
1683
- } catch (e) {
1684
- logger6.error(`\u63D2\u4EF6 ${id} \u52A0\u8F7D\u5931\u8D25:`, e);
1664
+ const sortedPluginIds = this.getSortedPluginIds();
1665
+ const missingDeps = /* @__PURE__ */ new Map();
1666
+ const idsToLoad = new Set(sortedPluginIds);
1667
+ for (const id of sortedPluginIds) {
1668
+ const plugin = this.plugins.get(id);
1669
+ if (plugin?.metadata.dependencies) {
1670
+ const missing = plugin.metadata.dependencies.filter((depId) => !this.runtimes.has(depId));
1671
+ if (missing.length > 0) {
1672
+ missingDeps.set(id, missing);
1673
+ idsToLoad.delete(id);
1674
+ this.runtimes.delete(id);
1675
+ }
1685
1676
  }
1686
1677
  }
1687
- }
1688
- for (const id of sortedPluginIds) {
1689
- const runtime = this.runtimes.get(id);
1690
- if (runtime) {
1691
- try {
1692
- await runtime.mount();
1693
- } catch (e) {
1694
- logger6.error(`\u63D2\u4EF6 ${id} \u6302\u8F7D\u5931\u8D25:`, e);
1678
+ if (missingDeps.size > 0) {
1679
+ missingDeps.forEach((deps, id) => {
1680
+ logger6.error(`\u63D2\u4EF6 ${id} \u65E0\u6CD5\u52A0\u8F7D\uFF0C\u7F3A\u5931\u4F9D\u8D56: ${deps.join(", ")}`);
1681
+ });
1682
+ }
1683
+ for (const id of sortedPluginIds) {
1684
+ if (!idsToLoad.has(id)) continue;
1685
+ const runtime = this.runtimes.get(id);
1686
+ if (runtime && runtime.status === "initial") {
1687
+ try {
1688
+ logger6.info(`[PluginManager] invoking onLoad for ${id}`);
1689
+ await runtime.load();
1690
+ logger6.info(`[PluginManager] onLoad completed for ${id}`);
1691
+ } catch (e) {
1692
+ logger6.error(`\u63D2\u4EF6 ${id} \u52A0\u8F7D\u5931\u8D25:`, e);
1693
+ }
1694
+ }
1695
+ }
1696
+ for (const id of sortedPluginIds) {
1697
+ if (!idsToLoad.has(id)) continue;
1698
+ const runtime = this.runtimes.get(id);
1699
+ if (runtime && (runtime.status === "loaded" || runtime.status === "initial")) {
1700
+ try {
1701
+ await runtime.mount();
1702
+ } catch (e) {
1703
+ logger6.error(`\u63D2\u4EF6 ${id} \u6302\u8F7D\u5931\u8D25:`, e);
1704
+ }
1695
1705
  }
1696
1706
  }
1707
+ } finally {
1708
+ this.isInitializing = false;
1709
+ this.notify();
1697
1710
  }
1698
1711
  }
1699
1712
  /**
@@ -1740,8 +1753,9 @@ var PluginManager = class {
1740
1753
  * 加载插件列表
1741
1754
  * @param configs 插件配置
1742
1755
  * @param registry 插件注册表 (动态导入函数)
1756
+ * @param notify 是否在加载完成后触发通知,默认为 true
1743
1757
  */
1744
- async loadPlugins(configs, registry) {
1758
+ async loadPlugins(configs, registry, notify = true) {
1745
1759
  logger6.info("\u5F00\u59CB\u52A0\u8F7D\u63D2\u4EF6...");
1746
1760
  const localLoadPromises = Object.entries(registry).map(async ([registryId, importFn]) => {
1747
1761
  try {
@@ -1775,7 +1789,9 @@ var PluginManager = class {
1775
1789
  this.register(plugin, false);
1776
1790
  }
1777
1791
  });
1778
- this.notify();
1792
+ if (notify) {
1793
+ this.notify();
1794
+ }
1779
1795
  logger6.info(`\u63D2\u4EF6\u52A0\u8F7D\u5B8C\u6210\uFF0C\u5171\u52A0\u8F7D ${this.plugins.size} \u4E2A\u63D2\u4EF6`);
1780
1796
  }
1781
1797
  /**
@@ -2620,7 +2636,7 @@ var usePluginLoader = (options) => {
2620
2636
  const { configManager: configManager2 } = await import("./config-manager-3TKURRUT.mjs");
2621
2637
  configManager2.set("system", options.systemConfig);
2622
2638
  }
2623
- await pluginManager.loadPlugins(pluginConfigs, finalRegistry);
2639
+ await pluginManager.loadPlugins(pluginConfigs, finalRegistry, false);
2624
2640
  await pluginManager.initPlugins(sharedContext);
2625
2641
  setPluginsLoaded(true);
2626
2642
  logger10.info("Plugins loaded successfully");