@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.
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { ApiConfig } from './api-port';
3
3
  import { EventBusPort } from './event-bus-port';
4
+ import { TypedStorage } from './storage-port';
4
5
  /**
5
6
  * 插件类型定义
6
7
  */
@@ -34,34 +35,55 @@ export declare const Slot: {
34
35
  export type SlotType = typeof Slot[keyof typeof Slot];
35
36
  export type SlotPosition = SlotType | string;
36
37
  /**
37
- * 插件扩展 (插槽)
38
+ * 插件扩展 (插槽注入配置)
38
39
  */
39
40
  export interface PluginExtension {
41
+ /** 插槽位置标识符 */
40
42
  slot: SlotPosition;
43
+ /** 要注入的 React 组件 */
41
44
  component: React.ComponentType<any>;
45
+ /** 排序权重,数值越小越靠前 */
42
46
  order?: number;
43
- /** @internal 插件 ID,由系统自动注入 */
47
+ /** @internal 插件 ID,由系统在加载时自动注入,用于溯源 */
44
48
  _pluginId?: string;
49
+ /** 扩展的元数据信息 */
45
50
  meta?: {
51
+ /** 显示图标 */
46
52
  icon?: React.ReactNode;
53
+ /** 显示标签文本 */
47
54
  label?: string;
55
+ /** 详细描述 */
48
56
  description?: string;
57
+ /** 唯一标识符,用于某些特定插槽的索引 */
49
58
  key?: string;
59
+ /** 允许其他自定义属性 */
50
60
  [key: string]: any;
51
61
  };
52
62
  }
63
+ /**
64
+ * 插件配置项定义
65
+ */
53
66
  export interface PluginConfigItem {
67
+ /** 配置键名 */
54
68
  key: string;
69
+ /** 配置类型 */
55
70
  type: 'string' | 'number' | 'boolean' | 'select';
71
+ /** 配置显示的标签 */
56
72
  label: string;
73
+ /** 配置描述信息 */
57
74
  description?: string;
75
+ /** 默认值 */
58
76
  default?: any;
77
+ /** 当类型为 select 时的选项列表 */
59
78
  options?: {
60
79
  label: string;
61
80
  value: any;
62
81
  }[];
82
+ /** 选择模式:支持多选或标签模式 (AntD 风格) */
63
83
  mode?: 'multiple' | 'tags';
84
+ /** 最小值 (针对 number 类型) */
64
85
  min?: number;
86
+ /** 最大值 (针对 number 类型) */
65
87
  max?: number;
66
88
  }
67
89
  /**
@@ -91,81 +113,127 @@ export interface PluginCapabilities {
91
113
  background?: boolean;
92
114
  [key: string]: boolean | undefined;
93
115
  }
116
+ /**
117
+ * 存储项数据结构定义 (Schema)
118
+ */
94
119
  export interface StorageItemSchema {
120
+ /** 存储键名 */
95
121
  key: string;
122
+ /** 数据类型 */
96
123
  type: 'string' | 'number' | 'boolean' | 'object' | 'array';
124
+ /** 默认值 */
97
125
  default?: any;
126
+ /** 描述信息 */
98
127
  description?: string;
128
+ /**
129
+ * 作用域
130
+ * @description 'plugin' 表示仅当前插件可见(带插件 ID 前缀),'shared' 表示全局共享
131
+ */
99
132
  scope?: 'plugin' | 'shared';
100
133
  }
134
+ /**
135
+ * 插件存储接口
136
+ * @description 包含私有存储和共享存储访问能力
137
+ */
138
+ export interface PluginStorage extends TypedStorage {
139
+ /** 获取插件私有存储数据 */
140
+ get: TypedStorage['get'];
141
+ /** 设置插件私有存储数据 */
142
+ set: TypedStorage['set'];
143
+ /** 移除插件私有存储数据 */
144
+ remove: TypedStorage['remove'];
145
+ /** 全局共享存储访问 */
146
+ shared: TypedStorage & {
147
+ /** 获取全局共享存储数据 */
148
+ get: TypedStorage['get'];
149
+ /** 设置全局共享存储数据 */
150
+ set: TypedStorage['set'];
151
+ /** 移除全局共享存储数据 */
152
+ remove: TypedStorage['remove'];
153
+ };
154
+ }
101
155
  /**
102
156
  * 插件生命周期 Hooks
157
+ * @description 插件可以在不同的生命周期阶段执行特定的逻辑
103
158
  */
104
159
  export interface PluginLifecycle {
105
160
  /**
106
161
  * 插件加载时调用
107
- * @description 用于初始化内部状态、注册服务等。此时 UI 尚未挂载。
162
+ * @description 在插件被扫描并注入内核时触发。用于初始化内部状态、注册服务、设置拦截器等。此时 UI 尚未挂载。
163
+ * @param context - 插件上下文对象,提供核心能力的访问
108
164
  */
109
165
  onLoad?: (context: PluginContext) => void | Promise<void>;
110
166
  /**
111
167
  * 插件挂载到 UI 时调用
168
+ * @description 当插件的 UI 组件(如有)被 React 挂载到 DOM 时触发。
169
+ * @param context - 插件上下文对象
112
170
  */
113
171
  onMount?: (context: PluginContext) => void;
114
172
  /**
115
173
  * 插件从 UI 卸载时调用
174
+ * @description 当插件的 UI 组件被销毁时触发。用于清理定时器、取消订阅等。
175
+ * @param context - 插件上下文对象
116
176
  */
117
177
  onUnmount?: (context: PluginContext) => void;
118
178
  /**
119
179
  * 插件配置发生变化时调用
180
+ * @description 当用户通过配置中心修改插件设置时触发。
181
+ * @param newConfig - 变更后的新配置对象
182
+ * @param oldConfig - 变更前的旧配置对象
120
183
  */
121
184
  onConfigChange?: (newConfig: any, oldConfig: any) => void;
122
185
  }
123
186
  /**
124
187
  * 插件上下文
125
- * @description 传递给插件生命周期钩子的对象,提供核心能力的访问
188
+ * @description 传递给插件生命周期钩子的核心对象,是插件访问宿主环境能力的唯一入口。
126
189
  */
127
190
  export interface PluginContext {
191
+ /** 当前插件的唯一标识符 */
128
192
  pluginId: string;
193
+ /** API 请求能力入口 */
129
194
  api: any;
195
+ /** 事件总线能力入口 */
130
196
  events: EventBusPort;
131
- storage: {
132
- get: <T = any>(key: string) => T | null;
133
- set: <T = any>(key: string, value: T) => void;
134
- remove: (key: string) => void;
135
- shared: {
136
- get: <T = any>(key: string) => T | null;
137
- set: <T = any>(key: string, value: T) => void;
138
- remove: (key: string) => void;
139
- };
140
- };
197
+ /** 存储管理能力入口 */
198
+ storage: PluginStorage;
199
+ /** 日志输出工具,自动携带插件 ID 前缀 */
141
200
  logger: {
142
201
  debug: (...args: any[]) => void;
143
202
  info: (...args: any[]) => void;
144
203
  warn: (...args: any[]) => void;
145
204
  error: (...args: any[]) => void;
146
205
  };
147
- /** 访问其他插件提供的服务 */
206
+ /**
207
+ * 访问其他插件提供的服务
208
+ * @param serviceName - 服务注册名称
209
+ * @returns 服务实例,如果不存在则返回 undefined
210
+ */
148
211
  getService: <T = any>(serviceName: string) => T | undefined;
149
- /** 注册自己的服务供他人使用 */
212
+ /**
213
+ * 注册当前插件的服务供他人使用
214
+ * @param serviceName - 唯一服务名称
215
+ * @param service - 服务实现对象
216
+ */
150
217
  registerService: (serviceName: string, service: any) => void;
151
- /** 宿主环境 Window (沙箱) */
218
+ /** 宿主环境 Window 的代理对象 (用于沙箱隔离) */
152
219
  window: WindowProxy;
153
220
  }
154
221
  /**
155
- * 完整的插件接口定义
222
+ * 完整的插件对象定义
156
223
  */
157
224
  export interface Plugin extends PluginLifecycle {
158
225
  /**
159
- * 插件 ID
160
- * @description 必须与 metadata.id 一致,且定义为只读(通常通过 getter 实现)
226
+ * 插件唯一标识符
227
+ * @description 必须与 metadata.id 一致,通常为只读。
161
228
  */
162
229
  readonly id: string;
230
+ /** 插件元数据配置 */
163
231
  metadata: PluginMetadata;
164
- /** 插件提供的组件(可选) */
232
+ /** 插件提供的功能组件集合 (可选) */
165
233
  components?: Record<string, React.ComponentType<any>>;
166
- /** 插件提供的工具函数(可选) */
234
+ /** 插件提供的工具函数集合 (可选) */
167
235
  utils?: Record<string, any>;
168
- /** 默认配置(可选) */
236
+ /** 插件的初始默认配置 (可选) */
169
237
  defaultConfig?: Record<string, any>;
170
238
  }
171
239
  /**
@@ -192,36 +260,49 @@ export declare abstract class BasePlugin implements Plugin {
192
260
  * @returns 完整的插件对象
193
261
  */
194
262
  export declare function definePlugin(plugin: Omit<Plugin, 'id'>): Plugin;
263
+ /**
264
+ * 插件元数据定义
265
+ * @description 描述插件的静态属性,用于插件市场展示、权限校验和内核加载参考。
266
+ */
195
267
  export interface PluginMetadata {
268
+ /** 插件唯一 ID (推荐反向域名格式,如 com.company.plugin) */
196
269
  id: string;
270
+ /** 插件显示名称 */
197
271
  name: string;
272
+ /** 插件版本号 (符合 SemVer 规范) */
198
273
  version: string;
274
+ /** 插件类型 */
199
275
  type: PluginType;
276
+ /** 插件功能描述 */
200
277
  description?: string;
278
+ /** 插件作者信息 */
201
279
  author?: string;
280
+ /** 插件图标 (React 组件或图标名称) */
202
281
  icon?: React.ReactNode;
203
282
  /** 插件依赖的其他插件 ID 列表 */
204
283
  dependencies?: string[];
205
- /** 路由配置 */
284
+ /** 路由配置集合,用于在主应用中注册页面 */
206
285
  routes?: RouteConfig[];
207
- /** 插槽扩展 */
286
+ /** 插槽扩展集合,用于在主应用 UI 预留位注入组件 */
208
287
  extensions?: PluginExtension[];
209
- /** API 配置 */
288
+ /** 插件所需调用的 API 接口配置 */
210
289
  api?: ApiConfig;
211
- /** 插件能力声明 */
290
+ /** 插件行为能力声明 */
212
291
  capabilities?: PluginCapabilities;
213
- /** 插件配置定义 */
292
+ /** 插件的可配置项定义 */
214
293
  configuration?: PluginConfigItem[];
215
- /** 存储定义(Schema) */
294
+ /** 插件所需的存储结构定义 */
216
295
  storage?: StorageItemSchema[];
217
296
  /**
218
- * 系统状态绑定
219
- * @description 将插件配置自动同步到系统 UI 状态
297
+ * 系统状态自动绑定
298
+ * @description 将插件的配置项自动同步到系统的全局状态中 (如主题色、身份认证等)
220
299
  */
221
300
  systemStateBindings?: {
301
+ /** 插件配置中的键名 */
222
302
  configKey: string;
303
+ /** 系统全局状态中的键名 */
223
304
  stateKey: string;
224
- /** 预设转换逻辑: 'theme-mode' | 'identity' 等 */
305
+ /** 预设的转换逻辑名称 */
225
306
  transform?: 'theme-mode' | 'identity';
226
307
  }[];
227
308
  }
@@ -1,19 +1,49 @@
1
1
  /**
2
2
  * 存储端口接口
3
- * @description 定义数据持久化的契约
3
+ * @description 定义数据持久化的标准契约,兼容 Web Storage API。
4
4
  */
5
5
  export interface StoragePort {
6
+ /**
7
+ * 获取存储项
8
+ * @param key - 键名
9
+ * @returns 对应的值,如果不存在则返回 null
10
+ */
6
11
  getItem(key: string): string | null;
12
+ /**
13
+ * 设置存储项
14
+ * @param key - 键名
15
+ * @param value - 键值 (字符串)
16
+ */
7
17
  setItem(key: string, value: string): void;
18
+ /**
19
+ * 移除指定的存储项
20
+ * @param key - 键名
21
+ */
8
22
  removeItem(key: string): void;
23
+ /**
24
+ * 清空所有存储项
25
+ */
9
26
  clear(): void;
10
27
  /**
11
- * 返回存储对象中存储的数据项数量。
28
+ * 返回存储对象中当前存储的数据项总数
12
29
  */
13
30
  readonly length: number;
14
31
  /**
15
- * 返回存储中第 n 个键的名称。
16
- * @param index 一个整数,表示要获取名称的键的编号。
32
+ * 根据索引返回存储中对应键的名称
33
+ * @param index - 数值索引
34
+ * @returns 键名,如果索引超出范围则返回 null
17
35
  */
18
36
  key(index: number): string | null;
19
37
  }
38
+ /**
39
+ * 类型化存储接口
40
+ * @description 提供泛型支持的存储访问能力,自动处理序列化
41
+ */
42
+ export interface TypedStorage {
43
+ /** 获取存储数据 */
44
+ get: <T = any>(key: string) => T | null;
45
+ /** 设置存储数据 */
46
+ set: <T = any>(key: string, value: T) => void;
47
+ /** 移除存储数据 */
48
+ remove: (key: string) => void;
49
+ }
@@ -2,60 +2,70 @@
2
2
  * 日志等级枚举
3
3
  */
4
4
  export declare enum LogLevel {
5
+ /** 调试级别:输出最详尽的信息 */
5
6
  DEBUG = 0,
7
+ /** 信息级别:输出重要的运行状态 */
6
8
  INFO = 1,
9
+ /** 警告级别:输出潜在的问题,但不影响运行 */
7
10
  WARN = 2,
11
+ /** 错误级别:输出严重的运行异常 */
8
12
  ERROR = 3,
13
+ /** 禁用日志:不输出任何信息 */
9
14
  NONE = 4
10
15
  }
11
16
  /**
12
17
  * 日志工具类
13
- * @description 支持日志等级控制和统一前缀格式,方便治理和排查问题
18
+ * @description 核心工具类,提供统一的日志输出格式 `[Prefix] Message`。
19
+ * 支持通过 `LogLevel` 进行全局过滤,在生产环境下建议设置为 WARN 或以上。
14
20
  */
15
21
  export declare class Logger {
16
- /** 全局默认日志等级 */
22
+ /** 全局静态日志等级,所有实例共享 */
17
23
  private static level;
18
- /** 当前实例的前缀 */
24
+ /** 当前实例的业务模块前缀 */
19
25
  private prefix;
20
26
  /**
21
27
  * 构造函数
22
- * @param prefix 日志前缀,默认为 'App'
28
+ * @param prefix - 业务模块前缀,默认为 'App'
23
29
  */
24
30
  constructor(prefix?: string);
25
31
  /**
26
- * 设置全局日志等级
27
- * @param level 日志等级
32
+ * 静态方法:设置全局日志输出等级
33
+ * @param level - 目标日志等级
28
34
  */
29
35
  static setLevel(level: LogLevel): void;
30
36
  /**
31
- * 获取当前日志等级
32
- * @returns 当前全局日志等级
37
+ * 静态方法:获取当前全局日志等级
38
+ * @returns 当前生效的全局日志等级
33
39
  */
34
40
  static getLevel(): LogLevel;
35
41
  /**
36
42
  * 打印 DEBUG 级别日志
43
+ * @description 仅在 LogLevel <= DEBUG 时输出
37
44
  */
38
45
  get debug(): (...args: any[]) => void;
39
46
  /**
40
47
  * 打印 INFO 级别日志
48
+ * @description 仅在 LogLevel <= INFO 时输出
41
49
  */
42
50
  get info(): (...args: any[]) => void;
43
51
  /**
44
52
  * 打印 WARN 级别日志
53
+ * @description 仅在 LogLevel <= WARN 时输出
45
54
  */
46
55
  get warn(): (...args: any[]) => void;
47
56
  /**
48
57
  * 打印 ERROR 级别日志
58
+ * @description 仅在 LogLevel <= ERROR 时输出
49
59
  */
50
60
  get error(): (...args: any[]) => void;
51
61
  /**
52
- * 开始一个日志分组
53
- * @param label 分组标签
54
- * @param collapsed 是否默认折叠,默认为 false
62
+ * 开始一个日志控制台分组
63
+ * @param label - 分组显示的标签名
64
+ * @param collapsed - 是否默认折叠显示,默认为 false
55
65
  */
56
66
  group(label: string, collapsed?: boolean): void;
57
67
  /**
58
- * 结束当前日志分组
68
+ * 结束当前日志控制台分组
59
69
  */
60
70
  get groupEnd(): () => void;
61
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chatbi-v/core",
3
- "version": "2.0.4",
3
+ "version": "2.1.0",
4
4
  "main": "dist/index.cjs",
5
5
  "files": [
6
6
  "dist"
@@ -26,10 +26,12 @@
26
26
  },
27
27
  "peerDependencies": {},
28
28
  "devDependencies": {
29
+ "@types/react": "^18.3.1",
30
+ "@types/react-dom": "^18.3.0",
29
31
  "tsup": "^8.5.1",
30
32
  "vite": "^5.4.21",
31
- "@chatbi-v/tsconfig": "2.0.4",
32
- "@chatbi-v/cli": "2.0.4"
33
+ "@chatbi-v/cli": "2.1.0",
34
+ "@chatbi-v/tsconfig": "2.1.0"
33
35
  },
34
36
  "scripts": {
35
37
  "build": "chatbi-cli build",