@chatbi-v/core 1.0.2

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.
Files changed (38) hide show
  1. package/README.md +36 -0
  2. package/dist/adapters/local-storage-adapter.d.ts +13 -0
  3. package/dist/adapters/scoped-storage-adapter.d.ts +18 -0
  4. package/dist/api/adapters/axios-adapter.d.ts +10 -0
  5. package/dist/api/engine.d.ts +41 -0
  6. package/dist/api/index.d.ts +6 -0
  7. package/dist/api/utils.d.ts +14 -0
  8. package/dist/api-context.d.ts +8 -0
  9. package/dist/application/service-registry.d.ts +34 -0
  10. package/dist/components/PluginErrorBoundary.d.ts +22 -0
  11. package/dist/components/PluginSlot.d.ts +22 -0
  12. package/dist/components/SlotSkeletons.d.ts +10 -0
  13. package/dist/config-manager.d.ts +30 -0
  14. package/dist/domain/auto-loader.d.ts +37 -0
  15. package/dist/domain/models.d.ts +9 -0
  16. package/dist/domain/plugin-manager.d.ts +143 -0
  17. package/dist/domain/plugin-runtime.d.ts +34 -0
  18. package/dist/domain/plugin-sandbox.d.ts +27 -0
  19. package/dist/domain/storage-manager.d.ts +46 -0
  20. package/dist/event-bus.d.ts +38 -0
  21. package/dist/hooks/use-plugin-loader.d.ts +20 -0
  22. package/dist/hooks/use-storage-state.d.ts +15 -0
  23. package/dist/index.cjs +2265 -0
  24. package/dist/index.d.ts +24 -0
  25. package/dist/index.mjs +2210 -0
  26. package/dist/plugin-context.d.ts +8 -0
  27. package/dist/ports/api-port.d.ts +71 -0
  28. package/dist/ports/event-bus-port.d.ts +30 -0
  29. package/dist/ports/plugin-port.d.ts +192 -0
  30. package/dist/ports/storage-port.d.ts +19 -0
  31. package/dist/sandbox/proxy-sandbox.d.ts +45 -0
  32. package/dist/sandbox/style-isolation.d.ts +13 -0
  33. package/dist/testing/plugin-contract.d.ts +6 -0
  34. package/dist/utils/date.d.ts +32 -0
  35. package/dist/utils/index.d.ts +4 -0
  36. package/dist/utils/logger.d.ts +69 -0
  37. package/dist/utils/stream-parser.d.ts +26 -0
  38. package/package.json +39 -0
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { PluginManager } from './domain/plugin-manager';
3
+ export interface PluginProviderProps {
4
+ manager: PluginManager;
5
+ children: React.ReactNode;
6
+ }
7
+ export declare const PluginProvider: React.FC<PluginProviderProps>;
8
+ export declare const usePluginManager: () => PluginManager;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * HTTP 请求方法
3
+ */
4
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
5
+ /**
6
+ * 错误处理策略
7
+ */
8
+ export type ErrorStrategy = 'reject' | 'resolve' | 'silent';
9
+ export interface ApiEndpointConfig {
10
+ /** 接口路径 (支持 :param 语法) */
11
+ url: string;
12
+ /** 请求方法 */
13
+ method: HttpMethod;
14
+ /** 描述 */
15
+ desc?: string;
16
+ /** Mock 响应 Schema */
17
+ responseSchema?: any;
18
+ /** 错误处理策略 */
19
+ errorStrategy?: ErrorStrategy;
20
+ }
21
+ export interface ApiRequestConfig {
22
+ url: string;
23
+ method: HttpMethod;
24
+ data?: any;
25
+ params?: any;
26
+ headers?: any;
27
+ /** 用于取消请求的信号 */
28
+ signal?: AbortSignal;
29
+ }
30
+ /**
31
+ * 流式响应回调
32
+ */
33
+ export interface StreamCallbacks {
34
+ onMessage?: (data: any) => void;
35
+ onError?: (error: any) => void;
36
+ onFinish?: () => void;
37
+ }
38
+ /**
39
+ * API 适配器端口
40
+ * @description 所有请求引擎(Fetch, Axios, Mock)都必须实现此接口
41
+ */
42
+ export interface ApiAdapter {
43
+ request<T = any>(config: ApiRequestConfig, endpointConfig?: ApiEndpointConfig): Promise<T>;
44
+ /**
45
+ * 发起流式请求
46
+ */
47
+ stream?(config: ApiRequestConfig, callbacks: StreamCallbacks, endpointConfig?: ApiEndpointConfig): Promise<void>;
48
+ }
49
+ /**
50
+ * API 配置集合 (按模块分组)
51
+ */
52
+ export interface ApiConfig {
53
+ [module: string]: {
54
+ [action: string]: ApiEndpointConfig;
55
+ };
56
+ }
57
+ /**
58
+ * 请求选项
59
+ */
60
+ export interface RequestOptions extends Partial<ApiRequestConfig> {
61
+ /** 是否跳过错误处理 */
62
+ skipErrorHandler?: boolean;
63
+ /** 路径参数 (替换 url 中的 :param) */
64
+ params?: any;
65
+ /** 流式响应回调 */
66
+ onMessage?: (data: any) => void;
67
+ onError?: (error: any) => void;
68
+ onFinish?: () => void;
69
+ /** 允许其他属性 (例如 axios 配置) */
70
+ [key: string]: any;
71
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 事件总线端口接口
3
+ */
4
+ export interface EventBusPort {
5
+ /**
6
+ * 订阅事件
7
+ * @param event 事件名称
8
+ * @param callback 回调函数
9
+ * @returns 取消订阅函数
10
+ */
11
+ on(event: string, callback: (...args: any[]) => any): () => void;
12
+ /**
13
+ * 取消订阅
14
+ * @param event 事件名称
15
+ * @param callback 回调函数
16
+ */
17
+ off(event: string, callback: (...args: any[]) => any): void;
18
+ /**
19
+ * 触发事件
20
+ * @param event 事件名称
21
+ * @param args 事件参数
22
+ */
23
+ emit(event: string, ...args: any[]): void;
24
+ /**
25
+ * 订阅一次性事件
26
+ * @param event 事件名称
27
+ * @param callback 回调函数
28
+ */
29
+ once(event: string, callback: (...args: any[]) => any): void;
30
+ }
@@ -0,0 +1,192 @@
1
+ import React from 'react';
2
+ import { ApiConfig } from './api-port';
3
+ import { EventBusPort } from './event-bus-port';
4
+ /**
5
+ * 插件类型定义
6
+ */
7
+ export declare const PLUGIN_TYPES: readonly ["business", "functional", "view", "theme", "renderer", "system"];
8
+ export type PluginType = typeof PLUGIN_TYPES[number];
9
+ /**
10
+ * 路由配置
11
+ */
12
+ export interface RouteConfig {
13
+ path: string;
14
+ component: React.ComponentType<any>;
15
+ meta?: Record<string, any>;
16
+ }
17
+ /**
18
+ * 插件插槽位置
19
+ * @description 定义插件可以注入 UI 的标准位置
20
+ */
21
+ export declare const Slot: {
22
+ readonly Sidebar: "sidebar";
23
+ readonly SidebarPanel: "sidebar-panel";
24
+ readonly Header: "header";
25
+ readonly StatusBar: "status-bar";
26
+ readonly Settings: "settings";
27
+ readonly MessageRenderer: "message-renderer";
28
+ readonly MessageContentRenderer: "message-content-renderer";
29
+ readonly SidebarSystem: "sidebar-system";
30
+ readonly SidebarBottom: "sidebar-bottom";
31
+ readonly Custom: "custom";
32
+ };
33
+ export type SlotType = typeof Slot[keyof typeof Slot];
34
+ export type SlotPosition = SlotType | string;
35
+ /**
36
+ * 插件扩展 (插槽)
37
+ */
38
+ export interface PluginExtension {
39
+ slot: SlotPosition;
40
+ component: React.ComponentType<any>;
41
+ order?: number;
42
+ /** @internal 插件 ID,由系统自动注入 */
43
+ _pluginId?: string;
44
+ meta?: {
45
+ icon?: React.ReactNode;
46
+ label?: string;
47
+ description?: string;
48
+ key?: string;
49
+ [key: string]: any;
50
+ };
51
+ }
52
+ export interface PluginConfigItem {
53
+ key: string;
54
+ type: 'string' | 'number' | 'boolean' | 'select';
55
+ label: string;
56
+ description?: string;
57
+ default?: any;
58
+ options?: {
59
+ label: string;
60
+ value: any;
61
+ }[];
62
+ mode?: 'multiple' | 'tags';
63
+ min?: number;
64
+ max?: number;
65
+ }
66
+ /**
67
+ * 插件能力定义 (Behavioral Capabilities)
68
+ * @description 定义插件的行为特征。注意:路由(routes)和扩展(extensions)属于结构化能力,直接通过 metadata 字段声明,不在此列。
69
+ */
70
+ export interface PluginCapabilities {
71
+ /**
72
+ * 是否支持配置设置
73
+ * @default false (如果 metadata.configuration 存在,则可能被隐式视为 true,建议显式声明)
74
+ */
75
+ configurable?: boolean;
76
+ /**
77
+ * 是否可嵌入其他页面
78
+ * @description 声明该插件是否可以作为 Widget 被其他插件引用
79
+ */
80
+ embeddable?: boolean;
81
+ /**
82
+ * 是否支持多实例
83
+ * @description 默认为 false (单例)。如果在聊天窗口中每个会话都需要独立状态,则设为 true
84
+ */
85
+ multiInstance?: boolean;
86
+ /**
87
+ * 是否需要后台运行
88
+ * @description 如果为 true,即使 UI 不可见,插件也不会被卸载
89
+ */
90
+ background?: boolean;
91
+ [key: string]: boolean | undefined;
92
+ }
93
+ export interface StorageItemSchema {
94
+ key: string;
95
+ type: 'string' | 'number' | 'boolean' | 'object' | 'array';
96
+ default?: any;
97
+ description?: string;
98
+ scope?: 'plugin' | 'shared';
99
+ }
100
+ /**
101
+ * 插件生命周期 Hooks
102
+ */
103
+ export interface PluginLifecycle {
104
+ /**
105
+ * 插件加载时调用
106
+ * @description 用于初始化内部状态、注册服务等。此时 UI 尚未挂载。
107
+ */
108
+ onLoad?: (context: PluginContext) => void | Promise<void>;
109
+ /**
110
+ * 插件挂载到 UI 时调用
111
+ */
112
+ onMount?: (context: PluginContext) => void;
113
+ /**
114
+ * 插件从 UI 卸载时调用
115
+ */
116
+ onUnmount?: (context: PluginContext) => void;
117
+ /**
118
+ * 插件配置发生变化时调用
119
+ */
120
+ onConfigChange?: (newConfig: any, oldConfig: any) => void;
121
+ }
122
+ /**
123
+ * 插件上下文
124
+ * @description 传递给插件生命周期钩子的对象,提供核心能力的访问
125
+ */
126
+ export interface PluginContext {
127
+ pluginId: string;
128
+ api: any;
129
+ events: EventBusPort;
130
+ storage: {
131
+ get: <T = any>(key: string) => T | null;
132
+ set: <T = any>(key: string, value: T) => void;
133
+ remove: (key: string) => void;
134
+ shared: {
135
+ get: <T = any>(key: string) => T | null;
136
+ set: <T = any>(key: string, value: T) => void;
137
+ remove: (key: string) => void;
138
+ };
139
+ };
140
+ logger: {
141
+ debug: (...args: any[]) => void;
142
+ info: (...args: any[]) => void;
143
+ warn: (...args: any[]) => void;
144
+ error: (...args: any[]) => void;
145
+ };
146
+ /** 访问其他插件提供的服务 */
147
+ getService: <T = any>(serviceName: string) => T | undefined;
148
+ /** 注册自己的服务供他人使用 */
149
+ registerService: (serviceName: string, service: any) => void;
150
+ /** 宿主环境 Window (沙箱) */
151
+ window: WindowProxy;
152
+ }
153
+ /**
154
+ * 完整的插件接口定义
155
+ */
156
+ export interface Plugin extends PluginLifecycle {
157
+ /**
158
+ * 插件 ID
159
+ * @description 必须与 metadata.id 一致,方便快速访问
160
+ */
161
+ id: string;
162
+ metadata: PluginMetadata;
163
+ /** 插件提供的组件(可选) */
164
+ components?: Record<string, React.ComponentType<any>>;
165
+ /** 插件提供的工具函数(可选) */
166
+ utils?: Record<string, any>;
167
+ /** 默认配置(可选) */
168
+ defaultConfig?: Record<string, any>;
169
+ }
170
+ export interface PluginMetadata {
171
+ id: string;
172
+ name: string;
173
+ version: string;
174
+ type: PluginType;
175
+ description?: string;
176
+ author?: string;
177
+ icon?: React.ReactNode;
178
+ /** 插件依赖的其他插件 ID 列表 */
179
+ dependencies?: string[];
180
+ /** 路由配置 */
181
+ routes?: RouteConfig[];
182
+ /** 插槽扩展 */
183
+ extensions?: PluginExtension[];
184
+ /** API 配置 */
185
+ api?: ApiConfig;
186
+ /** 插件能力声明 */
187
+ capabilities?: PluginCapabilities;
188
+ /** 插件配置定义 */
189
+ configuration?: PluginConfigItem[];
190
+ /** 存储定义(Schema) */
191
+ storage?: StorageItemSchema[];
192
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * 存储端口接口
3
+ * @description 定义数据持久化的契约
4
+ */
5
+ export interface StoragePort {
6
+ getItem(key: string): string | null;
7
+ setItem(key: string, value: string): void;
8
+ removeItem(key: string): void;
9
+ clear(): void;
10
+ /**
11
+ * 返回存储对象中存储的数据项数量。
12
+ */
13
+ readonly length: number;
14
+ /**
15
+ * 返回存储中第 n 个键的名称。
16
+ * @param index 一个整数,表示要获取名称的键的编号。
17
+ */
18
+ key(index: number): string | null;
19
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * ProxySandbox 类
3
+ * @description 基于 Proxy 的 JS 沙箱实现,模拟独立的 Window 环境
4
+ */
5
+ export declare class ProxySandbox {
6
+ /** 沙箱名称 */
7
+ name: string;
8
+ /** 代理后的 Window 对象 */
9
+ proxy: WindowProxy;
10
+ /** 沙箱是否激活 */
11
+ running: boolean;
12
+ /** 记录新增/修改的全局变量 */
13
+ private updatedValueSet;
14
+ /** 副作用记录池 */
15
+ private effectPool;
16
+ /** 真实的 Window 对象 */
17
+ private globalContext;
18
+ /** 白名单全局变量(允许透传访问真实 Window) */
19
+ private static globalWhitelist;
20
+ constructor(name: string, globalContext?: Window & typeof globalThis);
21
+ /**
22
+ * 激活沙箱
23
+ */
24
+ active(): void;
25
+ /**
26
+ * 销毁沙箱
27
+ */
28
+ inactive(): void;
29
+ /**
30
+ * 在沙箱中执行代码
31
+ * @param code JS 代码字符串
32
+ * @returns 执行结果
33
+ */
34
+ eval(code: string): any;
35
+ /**
36
+ * 创建伪造的 Window 对象
37
+ */
38
+ private createFakeWindow;
39
+ private isConstructor;
40
+ private isNativeFunction;
41
+ /**
42
+ * 劫持全局副作用 API
43
+ */
44
+ private patchGlobalEffects;
45
+ }
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ interface SandboxContainerProps {
3
+ children?: React.ReactNode;
4
+ name: string;
5
+ className?: string;
6
+ style?: React.CSSProperties;
7
+ }
8
+ /**
9
+ * 样式隔离容器
10
+ * @description 使用 Shadow DOM 实现完全的样式隔离
11
+ */
12
+ export declare const SandboxContainer: React.FC<SandboxContainerProps>;
13
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Plugin } from '../ports/plugin-port';
2
+ /**
3
+ * 插件契约测试套件
4
+ * @description 验证插件实现是否符合核心契约
5
+ */
6
+ export declare function verifyPluginContract(plugin: Plugin): void;
@@ -0,0 +1,32 @@
1
+ import dayjs from 'dayjs';
2
+ import 'dayjs/locale/zh-cn';
3
+ /**
4
+ * 日期时间格式化工具类
5
+ */
6
+ export declare const dateUtils: {
7
+ /**
8
+ * 格式化日期为 YYYY-MM-DD
9
+ */
10
+ formatDate(date?: dayjs.ConfigType): string;
11
+ /**
12
+ * 格式化时间为 HH:mm:ss
13
+ */
14
+ formatTime(date?: dayjs.ConfigType): string;
15
+ /**
16
+ * 格式化日期时间为 YYYY-MM-DD HH:mm:ss
17
+ */
18
+ formatDateTime(date?: dayjs.ConfigType): string;
19
+ /**
20
+ * 获取当前时间戳(毫秒)
21
+ */
22
+ now(): number;
23
+ /**
24
+ * 获取相对时间(例如:几分钟前)
25
+ */
26
+ fromNow(date: dayjs.ConfigType): string;
27
+ /**
28
+ * 原始 dayjs 对象,用于更复杂的场景
29
+ */
30
+ dayjs: typeof dayjs;
31
+ };
32
+ export default dateUtils;
@@ -0,0 +1,4 @@
1
+ export * from './logger';
2
+ export * from './stream-parser';
3
+ export * from './date';
4
+ export declare const version = "1.0.0";
@@ -0,0 +1,69 @@
1
+ /**
2
+ * 日志等级枚举
3
+ */
4
+ export declare enum LogLevel {
5
+ DEBUG = 0,
6
+ INFO = 1,
7
+ WARN = 2,
8
+ ERROR = 3,
9
+ NONE = 4
10
+ }
11
+ /**
12
+ * 日志工具类
13
+ * @description 支持日志等级控制和统一前缀格式,方便治理和排查问题
14
+ */
15
+ export declare class Logger {
16
+ /** 全局默认日志等级 */
17
+ private static level;
18
+ /** 当前实例的前缀 */
19
+ private prefix;
20
+ /**
21
+ * 构造函数
22
+ * @param prefix 日志前缀,默认为 'App'
23
+ */
24
+ constructor(prefix?: string);
25
+ /**
26
+ * 设置全局日志等级
27
+ * @param level 日志等级
28
+ */
29
+ static setLevel(level: LogLevel): void;
30
+ /**
31
+ * 获取当前日志等级
32
+ * @returns 当前全局日志等级
33
+ */
34
+ static getLevel(): LogLevel;
35
+ /**
36
+ * 打印 DEBUG 级别日志
37
+ */
38
+ get debug(): (...args: any[]) => void;
39
+ /**
40
+ * 打印 INFO 级别日志
41
+ */
42
+ get info(): (...args: any[]) => void;
43
+ /**
44
+ * 打印 WARN 级别日志
45
+ */
46
+ get warn(): (...args: any[]) => void;
47
+ /**
48
+ * 打印 ERROR 级别日志
49
+ */
50
+ get error(): (...args: any[]) => void;
51
+ /**
52
+ * 开始一个日志分组
53
+ * @param label 分组标签
54
+ * @param collapsed 是否默认折叠,默认为 false
55
+ */
56
+ group(label: string, collapsed?: boolean): void;
57
+ /**
58
+ * 结束当前日志分组
59
+ */
60
+ get groupEnd(): () => void;
61
+ }
62
+ /** 默认 Logger 实例 */
63
+ export declare const logger: Logger;
64
+ /**
65
+ * 创建带特定前缀的 Logger 实例
66
+ * @param prefix 日志前缀
67
+ * @returns Logger 实例
68
+ */
69
+ export declare const createLogger: (prefix: string) => Logger;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 流数据事件接口
3
+ * @description 定义流式传输中的事件结构
4
+ */
5
+ export interface StreamEvent {
6
+ /** 事件类型:消息、数据、待办事项、日志或错误 */
7
+ event: 'message' | 'data' | 'todos' | 'log' | 'error' | 'think' | 'page';
8
+ /** 事件携带的数据 */
9
+ data: any;
10
+ }
11
+ /**
12
+ * 解析流数据块
13
+ * @description 将 SSE (Server-Sent Events) 格式的字符串解析为事件对象数组
14
+ *
15
+ * 支持的格式:
16
+ * 格式 1:
17
+ * event: 'message' | 'data' | 'todos' | 'error'
18
+ * data: object | string
19
+ *
20
+ * 格式 2:
21
+ * data: object | string
22
+ *
23
+ * @param chunk 接收到的流数据块字符串
24
+ * @returns 解析后的事件数组
25
+ */
26
+ export declare function parseStreamChunk(chunk: string): StreamEvent[];
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@chatbi-v/core",
3
+ "version": "1.0.2",
4
+ "main": "dist/index.mjs",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "module": "dist/index.mjs",
12
+ "types": "dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "dependencies": {
21
+ "axios": "^1.13.2"
22
+ },
23
+ "peerDependencies": {
24
+ "react": ">=18.0.0",
25
+ "react-dom": ">=18.0.0",
26
+ "@chatbi-v/cli": "1.0.2"
27
+ },
28
+ "devDependencies": {
29
+ "tsup": "^8.5.1",
30
+ "vite": "^5.4.21",
31
+ "@chatbi-v/tsconfig": "1.0.2"
32
+ },
33
+ "scripts": {
34
+ "build": "chatbi-cli build",
35
+ "dev": "chatbi-cli build --watch",
36
+ "test": "vitest",
37
+ "lint": "eslint src/"
38
+ }
39
+ }