@chatbi-v/core 2.0.4 → 2.1.0

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.
@@ -9,111 +9,167 @@ import { StoragePort } from '../ports/storage-port';
9
9
  import { StorageManager } from './storage-manager';
10
10
  /**
11
11
  * 插件管理器
12
- * @description 负责插件的加载、注册和生命周期管理
12
+ * @description 核心领域服务,负责插件的完整生命周期管理,包括扫描、注册、配置合并、状态切换及依赖解析。
13
13
  */
14
14
  export declare class PluginManager {
15
- /** 全局事件总线 */
15
+ /** 全局事件总线,用于插件间及插件与内核间的异步通信 */
16
16
  readonly eventBus: DefaultEventBus;
17
- /** 存储接口 */
17
+ /** 存储管理服务,负责插件私有存储和系统级状态的持久化 */
18
18
  private storageManager;
19
- /** 所有插件运行时实例 */
19
+ /** 插件 ID 到运行时实例的映射表 */
20
20
  private runtimes;
21
- /** 所有注册的插件(原始数据) */
21
+ /** 插件 ID 到原始插件定义对象的映射表 */
22
22
  private plugins;
23
- /** 收集的路由 */
23
+ /** 收集到的所有插件路由配置 */
24
24
  private routes;
25
- /** 收集的扩展点 */
25
+ /** 收集到的所有插件扩展点配置,按插槽位置分组 */
26
26
  private extensions;
27
- /** 插件状态管理(启用/禁用、排序) */
27
+ /** 插件的启用状态和排序信息的内存缓存 */
28
28
  private pluginStates;
29
- /** 状态变更监听器 */
29
+ /** 状态变更监听器集合,支持按插槽过滤 */
30
30
  private listeners;
31
- /** 共享上下文缓存 */
31
+ /** 按插槽位置存储的监听器,用于精确通知 */
32
+ private slotListeners;
33
+ /** 扩展点缓存,避免重复计算 */
34
+ private memoizedExtensions;
35
+ /** 路由缓存 */
36
+ private memoizedRoutes;
37
+ /** 传递给插件的共享上下文缓存 */
32
38
  private sharedContext;
33
- /** 收集的工具函数 */
39
+ /** 收集到的插件工具函数集合 */
34
40
  private utils;
41
+ /**
42
+ * 构造函数
43
+ * @param storage - 底层存储适配器
44
+ */
35
45
  constructor(storage: StoragePort);
46
+ /**
47
+ * 从持久化存储中恢复插件状态 (启用/禁用、排序等)
48
+ */
36
49
  private loadStates;
50
+ /**
51
+ * 将当前的插件状态持久化到存储中
52
+ */
37
53
  private saveStates;
38
54
  /**
39
- * 订阅插件状态变更
40
- * @param listener 监听函数
41
- * @returns 取消订阅函数
55
+ * 订阅插件状态的变更通知
56
+ * @param listener - 变更时的回调函数
57
+ * @param slot - (可选) 指定监听的插槽位置,若提供则仅在该插槽受影响时通知
58
+ * @returns 取消订阅的函数
59
+ */
60
+ subscribe(listener: () => void, slot?: SlotPosition | string): () => void;
61
+ /**
62
+ * 获取存储管理器实例
63
+ * @returns StorageManager 实例
42
64
  */
43
- subscribe(listener: () => void): () => void;
44
65
  getStorageManager(): StorageManager;
45
66
  /**
46
67
  * 触发状态变更通知
68
+ * @param affectedSlot - (可选) 受影响的插槽位置
47
69
  */
48
70
  private notify;
49
71
  /**
50
- * 获取所有已注册的插件
72
+ * 获取所有已注册的插件列表
73
+ * @description 结果会根据插件类型优先级和用户自定义排序进行排序
74
+ * @returns 排序后的插件数组
51
75
  */
52
76
  getPlugins(): Plugin[];
53
77
  /**
54
- * 获取插件状态
55
- * @param pluginId 插件 ID
78
+ * 获取指定插件的状态信息
79
+ * @param pluginId - 插件 ID
80
+ * @returns 包含启用状态和排序值的对象
56
81
  */
57
82
  getPluginState(pluginId: string): {
58
83
  enabled: boolean;
59
84
  order: number;
60
85
  };
61
86
  /**
62
- * 检查插件是否启用
63
- * @param pluginId 插件 ID
87
+ * 检查指定插件是否处于启用状态
88
+ * @param pluginId - 插件 ID
89
+ * @returns 是否启用
64
90
  */
65
91
  isPluginEnabled(pluginId: string): boolean;
66
92
  /**
67
- * 切换插件启用状态
68
- * @param pluginId 插件 ID
69
- * @param enabled 是否启用
93
+ * 切换插件的启用/禁用状态
94
+ * @description 禁用插件会立即触发其卸载生命周期并销毁运行时。
95
+ * @param pluginId - 插件 ID
96
+ * @param enabled - 目标状态
70
97
  */
71
98
  togglePlugin(pluginId: string, enabled: boolean): void;
72
99
  /**
73
- * 设置插件排序
74
- * @param pluginId 插件 ID
75
- * @param order 排序值
100
+ * 设置插件的显示排序权重
101
+ * @param pluginId - 插件 ID
102
+ * @param order - 排序权重值
76
103
  */
77
104
  setPluginOrder(pluginId: string, order: number): void;
78
105
  /**
79
- * 获取插件的统一能力描述
80
- * @param pluginId 插件 ID
106
+ * 获取指定插件的运行时状态
107
+ * @param pluginId - 插件 ID
108
+ * @returns 'error' | 'mounted' | 'loaded' | 'initial'
109
+ */
110
+ getPluginRuntimeStatus(pluginId: string): "error" | "mounted" | "loaded" | "initial";
111
+ /**
112
+ * 获取指定插件的运行时错误
113
+ * @param pluginId - 插件 ID
114
+ * @returns Error 对象或 null
115
+ */
116
+ getPluginError(pluginId: string): Error | null;
117
+ /**
118
+ * 报告插件运行时错误 (通常由 ErrorBoundary 调用)
119
+ * @param pluginId - 插件 ID
120
+ * @param error - 错误对象
121
+ */
122
+ reportPluginError(pluginId: string, error: Error): void;
123
+ /**
124
+ * 获取插件的完整能力声明
125
+ * @param pluginId - 插件 ID
126
+ * @returns 能力对象
81
127
  */
82
128
  getUnifiedCapabilities(pluginId: string): import("..").PluginCapabilities;
83
129
  /**
84
- * 更新插件配置
85
- * @param pluginId 插件 ID
86
- * @param key 配置项 Key
87
- * @param value 配置值
130
+ * 更新指定插件的某项配置
131
+ * @description 该操作会同步更新内存中的配置、持久化到存储并触发全局事件通知。
132
+ * @param pluginId - 插件 ID
133
+ * @param key - 配置键名
134
+ * @param value - 新的配置值
88
135
  */
89
136
  updatePluginConfig(pluginId: string, key: string, value: any): void;
90
137
  /**
91
- * 获取插件配置
92
- * @param pluginId 插件 ID
93
- * @param key 配置键
138
+ * 获取指定插件的某项配置值
139
+ * @param pluginId - 插件 ID
140
+ * @param key - 配置键名
94
141
  * @returns 配置值
95
142
  */
96
143
  getPluginConfig(pluginId: string, key: string): any;
97
144
  /**
98
- * 获取系统全局配置 (非插件特定配置)
99
- * @param key 配置键 (如 title, version)
145
+ * 获取系统全局配置 (非插件特定)
146
+ * @param key - 系统配置键名
147
+ * @returns 配置值
100
148
  */
101
149
  getSystemConfig(key: string): any;
102
150
  /**
103
- * 获取注册的服务
104
- * @param name 服务名称
151
+ * 获取由插件注册的服务实例
152
+ * @template T 服务接口类型
153
+ * @param name - 服务注册名称
154
+ * @returns 服务实例或 undefined
105
155
  */
106
156
  getService<T = any>(name: string): T | undefined;
107
157
  /**
108
- * 获取指定插槽的扩展
109
- * @param slot 插槽位置
158
+ * 获取指定插槽位置的所有已启用插件的扩展
159
+ * @param slot - 插槽位置标识
160
+ * @returns 排序后的扩展配置数组
110
161
  */
111
162
  getExtensions(slot: SlotPosition | string): PluginExtension[];
163
+ /**
164
+ * 获取所有已启用插件注册的路由配置
165
+ * @returns 增强后的路由配置数组
166
+ */
112
167
  getRoutes(): RouteConfig[];
113
168
  /**
114
- * 注册插件
115
- * @param plugin 插件实例
116
- * @param notify 是否触发状态变更通知
169
+ * 注册一个新插件到管理器中
170
+ * @description 此阶段会执行元数据校验、存储 Schema 注册、配置合并及扩展点收集。
171
+ * @param plugin - 插件对象
172
+ * @param notify - 是否在注册完成后触发状态变更通知
117
173
  */
118
174
  register(plugin: Plugin, notify?: boolean): void;
119
175
  /**
@@ -6,34 +6,65 @@
6
6
  import { Plugin, PluginContext } from '../ports/plugin-port';
7
7
  import { StorageManager } from './storage-manager';
8
8
  /**
9
- * 插件运行时
10
- * @description 封装单个插件的生命周期、沙箱和状态
9
+ * 插件运行时类
10
+ * @description 为单个插件提供独立的运行环境,负责管理其生命周期、初始化沙箱环境并组装上下文对象。
11
11
  */
12
12
  export declare class PluginRuntime {
13
+ /** 插件定义对象 */
13
14
  plugin: Plugin;
15
+ /** 传递给插件的上下文对象 */
14
16
  context: PluginContext;
17
+ /** 存储沙箱隔离设施 */
15
18
  private storageSandbox;
19
+ /** Window/全局对象沙箱隔离设施 */
16
20
  private windowSandbox;
21
+ /** 是否已完成加载阶段 */
17
22
  private isLoaded;
23
+ /** 是否已完成挂载阶段 */
18
24
  private isMounted;
25
+ /** 运行时捕获的错误信息 */
26
+ private error;
27
+ /**
28
+ * 构造函数
29
+ * @param plugin - 插件定义对象
30
+ * @param sharedContext - 来自内核的共享上下文资源 (如 API 引擎、事件总线)
31
+ * @param storageManager - 全局存储管理器
32
+ */
19
33
  constructor(plugin: Plugin, sharedContext: Record<string, any>, storageManager: StorageManager);
20
34
  /**
21
- * 加载插件 (onLoad)
22
- * @description 注册服务、初始化非 UI 逻辑
35
+ * 执行插件的加载逻辑 (onLoad)
36
+ * @description 此阶段会自动注册插件声明的 API 配置,并调用插件的 onLoad 钩子。
37
+ * 用于执行非 UI 的初始化逻辑,如注册服务、拦截器等。
23
38
  */
24
39
  load(): Promise<void>;
25
40
  /**
26
- * 挂载插件 (onMount)
27
- * @description 激活沙箱、处理 UI 相关的副作用
41
+ * 执行插件的挂载逻辑 (onMount)
42
+ * @description 此阶段会激活 Window 沙箱并调用插件的 onMount 钩子。
43
+ * 此时插件的 UI 组件(如有)即将或已经进入 DOM。
28
44
  */
29
45
  mount(): Promise<void>;
30
46
  /**
31
- * 卸载插件 (onUnmount)
47
+ * 执行插件的卸载逻辑 (onUnmount)
48
+ * @description 调用插件的 onUnmount 钩子并停用沙箱。
32
49
  */
33
50
  unmount(): Promise<void>;
34
51
  /**
35
- * 销毁插件 (完全移除)
52
+ * 彻底销毁插件实例
53
+ * @description 卸载插件并重置加载状态。
36
54
  */
37
55
  destroy(): Promise<void>;
38
- get status(): "mounted" | "loaded" | "initial";
56
+ /**
57
+ * 设置运行时错误 (内部使用)
58
+ * @internal
59
+ */
60
+ _setError(error: Error | null): void;
61
+ /**
62
+ * 获取插件运行时错误
63
+ */
64
+ getError(): Error | null;
65
+ /**
66
+ * 获取插件当前运行状态
67
+ * @returns 'error' | 'mounted' | 'loaded' | 'initial'
68
+ */
69
+ get status(): "error" | "mounted" | "loaded" | "initial";
39
70
  }
@@ -1,14 +1,26 @@
1
1
  import { StorageManager } from './storage-manager';
2
2
  /**
3
- * 插件沙箱
4
- * @description 为插件提供隔离的运行时环境
3
+ * 插件隔离沙箱
4
+ * @description 核心领域服务,为每个插件实例创建独立的运行上下文。
5
+ * 它是插件与系统核心之间的“中间层”,确保插件只能访问其权限范围内的资源。
6
+ * 主要职责:
7
+ * 1. 存储隔离:确保插件只能读写属于自己的 LocalStorage 命名空间。
8
+ * 2. 日志隔离:为插件提供带有自身 ID 前缀的日志输出,方便调试。
5
9
  */
6
10
  export declare class PluginSandbox {
11
+ /** 关联的插件唯一标识 */
7
12
  private pluginId;
13
+ /** 系统全局存储管理器 */
8
14
  private storageManager;
15
+ /**
16
+ * 构造插件沙箱
17
+ * @param pluginId - 插件 ID
18
+ * @param storageManager - 系统存储管理器实例
19
+ */
9
20
  constructor(pluginId: string, storageManager: StorageManager);
10
21
  /**
11
22
  * 获取隔离的存储接口
23
+ * @description 返回一个受限的 StoragePort,所有操作都会自动带上插件 ID 前缀。
12
24
  */
13
25
  get storage(): {
14
26
  shared: {
@@ -22,6 +34,7 @@ export declare class PluginSandbox {
22
34
  };
23
35
  /**
24
36
  * 获取隔离的日志接口
37
+ * @description 返回一个带插件 ID 前缀的 Logger 实例。
25
38
  */
26
39
  get logger(): import("..").Logger;
27
40
  }
@@ -1,37 +1,65 @@
1
1
  import { StorageItemSchema } from '../ports/plugin-port';
2
2
  import { StoragePort } from '../ports/storage-port';
3
3
  /**
4
- * 存储管理器
5
- * @description 统一管理系统、插件和共享存储
4
+ * 核心领域服务:存储管理器
5
+ * @description 统一管理应用内所有的持久化存储资源。
6
+ * 核心功能:
7
+ * 1. 作用域隔离:通过前缀划分 `plugin`、`shared` 和 `system` 三个层级的存储空间。
8
+ * 2. Schema 校验:支持注册存储描述,规范数据存取,避免键名冲突。
9
+ * 3. 默认值与配置回退:支持从 Schema 默认值或 ConfigManager 中回退取值。
6
10
  */
7
11
  export declare class StorageManager {
12
+ /** 底层物理存储驱动 */
8
13
  private baseStorage;
14
+ /** 插件 ID 与存储描述定义的映射关系 */
9
15
  private schemas;
16
+ /** 内存缓存,减少 JSON 序列化和物理 IO 开销 */
17
+ private memoryCache;
18
+ /**
19
+ * 初始化存储管理器
20
+ * @param baseStorage - 符合 StoragePort 接口的物理存储驱动(如 LocalStorageAdapter)
21
+ */
10
22
  constructor(baseStorage: StoragePort);
11
23
  /**
12
- * 注册插件存储 Schema
24
+ * 注册插件的存储 Schema
25
+ * @param pluginId - 插件 ID
26
+ * @param schema - 存储项定义列表
13
27
  */
14
28
  registerSchema(pluginId: string, schema: StorageItemSchema[]): void;
15
29
  /**
16
- * 验证 Key 是否符合 Schema 定义 (仅在开发环境或严格模式下警告)
30
+ * 内部校验方法:检查键名是否在 Schema 中声明
31
+ * @param pluginId - 插件 ID
32
+ * @param key - 待校验的键名
33
+ * @param scope - 目标作用域
17
34
  */
18
35
  private validateKey;
19
36
  /**
20
- * 获取插件专用存储适配器
21
- * @param pluginId 插件 ID
37
+ * 获取插件私有存储适配器
38
+ * @param pluginId - 插件 ID
39
+ * @returns 自动添加 `plugin:{id}:` 前缀的存储接口
22
40
  */
23
41
  getPluginStorage(pluginId: string): StoragePort;
24
42
  /**
25
- * 获取共享存储适配器
43
+ * 获取全局共享存储适配器
44
+ * @returns 自动添加 `shared:` 前缀的存储接口
26
45
  */
27
46
  getSharedStorage(): StoragePort;
28
47
  /**
29
- * 获取系统存储适配器
48
+ * 获取系统内部存储适配器
49
+ * @returns 自动添加 `system:` 前缀的存储接口
30
50
  */
31
51
  getSystemStorage(): StoragePort;
32
52
  /**
33
- * 获取插件上下文中的 storage 对象 (包含类型转换辅助方法)
34
- * @param pluginId 插件 ID
53
+ * 创建插件沙箱使用的复合存储对象
54
+ * @description 返回的对象封装了对私有存储和共享存储的操作。
55
+ * 特性:
56
+ * - 内存级 LRU 缓存:极大提升高频读写性能。
57
+ * - 自动序列化/反序列化 JSON。
58
+ * - 自动校验 Schema。
59
+ * - 取值回退逻辑:持久化存储 -> ConfigManager -> Schema 默认值。
60
+ *
61
+ * @param pluginId - 插件 ID
62
+ * @returns 包含 get/set/remove 及 shared 子对象的复合接口
35
63
  */
36
64
  getContextStorage(pluginId: string): {
37
65
  shared: {
@@ -1,22 +1,33 @@
1
+ /** 插件加载 Hook 的配置选项 */
1
2
  export interface PluginLoaderOptions {
2
- /** 插件发现规则 */
3
+ /** 插件发现规则,定义如何从模块列表中识别插件 */
3
4
  discoveryRules?: any[];
4
- /** 插件模块映射 (由 Vite import.meta.glob 生成) */
5
+ /** 插件模块映射,通常由 Vite 的 `import.meta.glob` 生成,键为路径,值为加载函数 */
5
6
  modules?: Record<string, () => Promise<any>>;
6
- /** 预注册的插件注册表 (可选,用于离线或手动加载) */
7
+ /** 预注册的插件注册表,用于手动指定插件加载逻辑,优先级高于自动发现 */
7
8
  registry?: Record<string, () => Promise<any>>;
8
- /** 插件配置映射 */
9
+ /** 插件的业务配置映射,键为插件 ID */
9
10
  pluginConfigs: Record<string, any>;
10
- /** 初始化的上下文服务 (如 api, events 等) */
11
+ /** 初始化的共享上下文服务,将注入到每个插件的 onLoad */
11
12
  sharedContext?: Record<string, any>;
12
- /** 系统级配置 (title, version ) */
13
+ /** 系统级全局配置,如标题、版本号、Logo 等 */
13
14
  systemConfig?: Record<string, any>;
14
- /** 发现的基础 URL */
15
+ /** 资源发现的基础 URL,默认为当前 window.location.origin */
15
16
  baseUrl?: string;
16
17
  }
17
18
  /**
18
- * 通用插件加载 Hook
19
- * @description 负责插件的自动发现、加载、初始化和状态订阅
19
+ * 核心 Hook:通用插件加载器
20
+ * @description 负责应用启动时的插件全生命周期管理:
21
+ * 1. 自动发现:解析 modules 并根据规则识别插件。
22
+ * 2. 注册:将插件信息录入 PluginManager。
23
+ * 3. 加载:调用插件的加载逻辑 (onLoad)。
24
+ * 4. 初始化:调用插件的挂载逻辑 (onMount)。
25
+ * 5. 响应式更新:订阅插件状态变更并触发 UI 刷新。
26
+ *
27
+ * @param options - 加载配置项
28
+ * @returns { pluginsLoaded: boolean, pluginVersion: number }
29
+ * - pluginsLoaded: 插件是否已全部完成初始化
30
+ * - pluginVersion: 插件状态版本号,用于强制触发依赖插件状态的组件重渲染
20
31
  */
21
32
  export declare const usePluginLoader: (options: PluginLoaderOptions) => {
22
33
  pluginsLoaded: boolean;