@chatbi-v/core 2.0.0 → 2.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.
- package/README.md +45 -20
- package/dist/domain/auto-loader.d.ts +3 -19
- package/dist/domain/plugin-manager.d.ts +9 -3
- package/dist/domain/plugin-runtime.d.ts +5 -0
- package/dist/hooks/use-plugin-loader.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +145 -78
- package/dist/index.plugin.js +5106 -0
- package/dist/ports/plugin-port.d.ts +26 -2
- package/package.json +4 -4
- package/dist/index.cjs +0 -2118
package/dist/index.cjs
DELETED
|
@@ -1,2118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __esm = (fn, res) => function __init() {
|
|
9
|
-
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
-
};
|
|
11
|
-
var __export = (target, all) => {
|
|
12
|
-
for (var name in all)
|
|
13
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
-
};
|
|
15
|
-
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
-
for (let key of __getOwnPropNames(from))
|
|
18
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
-
|
|
33
|
-
// src/config-manager.ts
|
|
34
|
-
var config_manager_exports = {};
|
|
35
|
-
__export(config_manager_exports, {
|
|
36
|
-
ConfigManager: () => ConfigManager,
|
|
37
|
-
configManager: () => configManager
|
|
38
|
-
});
|
|
39
|
-
var ConfigManager, configManager;
|
|
40
|
-
var init_config_manager = __esm({
|
|
41
|
-
"src/config-manager.ts"() {
|
|
42
|
-
"use strict";
|
|
43
|
-
ConfigManager = class {
|
|
44
|
-
/** 存储配置的 Map */
|
|
45
|
-
config = /* @__PURE__ */ new Map();
|
|
46
|
-
/**
|
|
47
|
-
* 设置配置项
|
|
48
|
-
* @param key 配置键
|
|
49
|
-
* @param value 配置值
|
|
50
|
-
*/
|
|
51
|
-
set(key, value) {
|
|
52
|
-
this.config.set(key, value);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* 获取配置项
|
|
56
|
-
* @param key 配置键
|
|
57
|
-
*/
|
|
58
|
-
get(key) {
|
|
59
|
-
return this.config.get(key);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* 合并配置
|
|
63
|
-
* @param config 配置对象
|
|
64
|
-
*/
|
|
65
|
-
merge(config) {
|
|
66
|
-
Object.keys(config).forEach((key) => {
|
|
67
|
-
const existing = this.config.get(key);
|
|
68
|
-
const incoming = config[key];
|
|
69
|
-
if (existing && typeof existing === "object" && incoming && typeof incoming === "object") {
|
|
70
|
-
this.config.set(key, { ...existing, ...incoming });
|
|
71
|
-
} else {
|
|
72
|
-
this.config.set(key, incoming);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* 获取所有配置
|
|
78
|
-
*/
|
|
79
|
-
getAll() {
|
|
80
|
-
return Object.fromEntries(this.config);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
configManager = new ConfigManager();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// src/index.ts
|
|
88
|
-
var index_exports = {};
|
|
89
|
-
__export(index_exports, {
|
|
90
|
-
ApiEngine: () => ApiEngine,
|
|
91
|
-
ApiProvider: () => ApiProvider,
|
|
92
|
-
AvatarSkeleton: () => AvatarSkeleton,
|
|
93
|
-
AxiosAdapter: () => AxiosAdapter,
|
|
94
|
-
BlockSkeleton: () => BlockSkeleton,
|
|
95
|
-
ConfigManager: () => ConfigManager,
|
|
96
|
-
DefaultEventBus: () => DefaultEventBus,
|
|
97
|
-
LocalStorageAdapter: () => LocalStorageAdapter,
|
|
98
|
-
LogLevel: () => LogLevel,
|
|
99
|
-
Logger: () => Logger,
|
|
100
|
-
PLUGIN_TYPES: () => PLUGIN_TYPES,
|
|
101
|
-
PluginErrorBoundary: () => PluginErrorBoundary,
|
|
102
|
-
PluginManager: () => PluginManager,
|
|
103
|
-
PluginProvider: () => PluginProvider,
|
|
104
|
-
PluginRuntime: () => PluginRuntime,
|
|
105
|
-
PluginSandbox: () => PluginSandbox,
|
|
106
|
-
PluginSlot: () => PluginSlot,
|
|
107
|
-
SUCCESS_CODE: () => SUCCESS_CODE,
|
|
108
|
-
ScopedStorageAdapter: () => ScopedStorageAdapter,
|
|
109
|
-
ServiceRegistry: () => ServiceRegistry,
|
|
110
|
-
SidebarIconSkeleton: () => SidebarIconSkeleton,
|
|
111
|
-
Slot: () => Slot,
|
|
112
|
-
StatusBarItemSkeleton: () => StatusBarItemSkeleton,
|
|
113
|
-
StorageManager: () => StorageManager,
|
|
114
|
-
apiEngine: () => apiEngine,
|
|
115
|
-
cleanUrlParams: () => cleanUrlParams,
|
|
116
|
-
configManager: () => configManager,
|
|
117
|
-
createLogger: () => createLogger,
|
|
118
|
-
dateUtils: () => dateUtils,
|
|
119
|
-
discoverPlugins: () => discoverPlugins,
|
|
120
|
-
isMockMode: () => isMockMode,
|
|
121
|
-
logger: () => logger,
|
|
122
|
-
normalizeParams: () => normalizeParams,
|
|
123
|
-
pluginManager: () => pluginManager,
|
|
124
|
-
resolveApiModules: () => resolveApiModules,
|
|
125
|
-
resolvePluginRegistry: () => resolvePluginRegistry,
|
|
126
|
-
serviceRegistry: () => serviceRegistry,
|
|
127
|
-
useApi: () => useApi,
|
|
128
|
-
usePluginLoader: () => usePluginLoader,
|
|
129
|
-
usePluginManager: () => usePluginManager,
|
|
130
|
-
useStorageState: () => useStorageState,
|
|
131
|
-
version: () => version
|
|
132
|
-
});
|
|
133
|
-
module.exports = __toCommonJS(index_exports);
|
|
134
|
-
|
|
135
|
-
// src/ports/plugin-port.ts
|
|
136
|
-
var PLUGIN_TYPES = ["business", "functional", "view", "theme", "renderer", "system"];
|
|
137
|
-
var Slot = {
|
|
138
|
-
Sidebar: "sidebar",
|
|
139
|
-
SidebarPanel: "sidebar-panel",
|
|
140
|
-
Header: "header",
|
|
141
|
-
StatusBar: "status-bar",
|
|
142
|
-
Settings: "settings",
|
|
143
|
-
MessageRenderer: "message-renderer",
|
|
144
|
-
MessageContentRenderer: "message-content-renderer",
|
|
145
|
-
SidebarSystem: "sidebar-system",
|
|
146
|
-
SidebarBottom: "sidebar-bottom",
|
|
147
|
-
RootLayout: "root-layout",
|
|
148
|
-
Custom: "custom"
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// src/utils/logger.ts
|
|
152
|
-
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
153
|
-
LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
|
|
154
|
-
LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
|
|
155
|
-
LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
|
|
156
|
-
LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
|
|
157
|
-
LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
|
|
158
|
-
return LogLevel2;
|
|
159
|
-
})(LogLevel || {});
|
|
160
|
-
var Logger = class _Logger {
|
|
161
|
-
/** 全局默认日志等级 */
|
|
162
|
-
static level = 1 /* INFO */;
|
|
163
|
-
/** 当前实例的前缀 */
|
|
164
|
-
prefix;
|
|
165
|
-
/**
|
|
166
|
-
* 构造函数
|
|
167
|
-
* @param prefix 日志前缀,默认为 'App'
|
|
168
|
-
*/
|
|
169
|
-
constructor(prefix = "App") {
|
|
170
|
-
this.prefix = prefix;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* 设置全局日志等级
|
|
174
|
-
* @param level 日志等级
|
|
175
|
-
*/
|
|
176
|
-
static setLevel(level) {
|
|
177
|
-
this.level = level;
|
|
178
|
-
console.info(`[Logger] Global log level set to: ${LogLevel[level]}`);
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* 获取当前日志等级
|
|
182
|
-
* @returns 当前全局日志等级
|
|
183
|
-
*/
|
|
184
|
-
static getLevel() {
|
|
185
|
-
return this.level;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* 打印 DEBUG 级别日志
|
|
189
|
-
*/
|
|
190
|
-
get debug() {
|
|
191
|
-
return _Logger.level <= 0 /* DEBUG */ ? console.debug.bind(console, `[${this.prefix}]`) : noop;
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* 打印 INFO 级别日志
|
|
195
|
-
*/
|
|
196
|
-
get info() {
|
|
197
|
-
return _Logger.level <= 1 /* INFO */ ? console.info.bind(console, `[${this.prefix}]`) : noop;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* 打印 WARN 级别日志
|
|
201
|
-
*/
|
|
202
|
-
get warn() {
|
|
203
|
-
return _Logger.level <= 2 /* WARN */ ? console.warn.bind(console, `[${this.prefix}]`) : noop;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* 打印 ERROR 级别日志
|
|
207
|
-
*/
|
|
208
|
-
get error() {
|
|
209
|
-
return _Logger.level <= 3 /* ERROR */ ? console.error.bind(console, `[${this.prefix}]`) : noop;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* 开始一个日志分组
|
|
213
|
-
* @param label 分组标签
|
|
214
|
-
* @param collapsed 是否默认折叠,默认为 false
|
|
215
|
-
*/
|
|
216
|
-
group(label, collapsed = false) {
|
|
217
|
-
if (_Logger.level <= 1 /* INFO */) {
|
|
218
|
-
if (collapsed) {
|
|
219
|
-
console.groupCollapsed(`[${this.prefix}] ${label}`);
|
|
220
|
-
} else {
|
|
221
|
-
console.group(`[${this.prefix}] ${label}`);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* 结束当前日志分组
|
|
227
|
-
*/
|
|
228
|
-
get groupEnd() {
|
|
229
|
-
return _Logger.level <= 1 /* INFO */ ? console.groupEnd.bind(console) : noop;
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
var noop = () => {
|
|
233
|
-
};
|
|
234
|
-
var logger = new Logger();
|
|
235
|
-
var createLogger = (prefix) => new Logger(prefix);
|
|
236
|
-
|
|
237
|
-
// src/application/service-registry.ts
|
|
238
|
-
var logger2 = createLogger("ServiceRegistry");
|
|
239
|
-
var ServiceRegistry = class {
|
|
240
|
-
services = /* @__PURE__ */ new Map();
|
|
241
|
-
listeners = /* @__PURE__ */ new Map();
|
|
242
|
-
/**
|
|
243
|
-
* 注册服务
|
|
244
|
-
* @param name 服务名称 (建议格式: pluginId.serviceName)
|
|
245
|
-
* @param service 服务实现
|
|
246
|
-
*/
|
|
247
|
-
register(name, service) {
|
|
248
|
-
if (this.services.has(name)) {
|
|
249
|
-
logger2.warn(`\u670D\u52A1 "${name}" \u5DF2\u5B58\u5728\uFF0C\u5C06\u88AB\u8986\u76D6`);
|
|
250
|
-
}
|
|
251
|
-
this.services.set(name, service);
|
|
252
|
-
logger2.info(`\u670D\u52A1\u5DF2\u6CE8\u518C: ${name}`);
|
|
253
|
-
if (this.listeners.has(name)) {
|
|
254
|
-
const set = this.listeners.get(name);
|
|
255
|
-
set.forEach((callback) => callback(service));
|
|
256
|
-
this.listeners.delete(name);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* 获取服务
|
|
261
|
-
* @param name 服务名称
|
|
262
|
-
*/
|
|
263
|
-
get(name) {
|
|
264
|
-
return this.services.get(name);
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* 异步获取服务 (如果尚未注册则等待)
|
|
268
|
-
* @param name 服务名称
|
|
269
|
-
*/
|
|
270
|
-
async waitFor(name) {
|
|
271
|
-
const service = this.services.get(name);
|
|
272
|
-
if (service) return service;
|
|
273
|
-
return new Promise((resolve) => {
|
|
274
|
-
if (!this.listeners.has(name)) {
|
|
275
|
-
this.listeners.set(name, /* @__PURE__ */ new Set());
|
|
276
|
-
}
|
|
277
|
-
this.listeners.get(name).add(resolve);
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* 注销服务
|
|
282
|
-
* @param name 服务名称
|
|
283
|
-
*/
|
|
284
|
-
unregister(name) {
|
|
285
|
-
this.services.delete(name);
|
|
286
|
-
logger2.info(`\u670D\u52A1\u5DF2\u6CE8\u9500: ${name}`);
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* 清除所有服务 (通常在系统重置时使用)
|
|
290
|
-
*/
|
|
291
|
-
clear() {
|
|
292
|
-
this.services.clear();
|
|
293
|
-
this.listeners.clear();
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
var serviceRegistry = new ServiceRegistry();
|
|
297
|
-
|
|
298
|
-
// src/api-context.tsx
|
|
299
|
-
var import_react = require("react");
|
|
300
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
301
|
-
var ApiContext = (0, import_react.createContext)(null);
|
|
302
|
-
var ApiProvider = ({ api, children }) => {
|
|
303
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ApiContext.Provider, { value: api, children });
|
|
304
|
-
};
|
|
305
|
-
var useApi = () => {
|
|
306
|
-
const context = (0, import_react.useContext)(ApiContext);
|
|
307
|
-
if (!context) {
|
|
308
|
-
throw new Error("useApi must be used within an ApiProvider");
|
|
309
|
-
}
|
|
310
|
-
return context;
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
// src/index.ts
|
|
314
|
-
init_config_manager();
|
|
315
|
-
|
|
316
|
-
// src/components/PluginErrorBoundary.tsx
|
|
317
|
-
var import_react2 = require("react");
|
|
318
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
319
|
-
var logger3 = createLogger("PluginErrorBoundary");
|
|
320
|
-
var PluginErrorBoundary = class extends import_react2.Component {
|
|
321
|
-
constructor(props) {
|
|
322
|
-
super(props);
|
|
323
|
-
this.state = { hasError: false, error: null };
|
|
324
|
-
}
|
|
325
|
-
static getDerivedStateFromError(error) {
|
|
326
|
-
return { hasError: true, error };
|
|
327
|
-
}
|
|
328
|
-
componentDidCatch(error, errorInfo) {
|
|
329
|
-
logger3.error(`\u63D2\u4EF6 ${this.props.pluginId || "\u672A\u77E5"} \u53D1\u751F\u9519\u8BEF:`, error, errorInfo);
|
|
330
|
-
}
|
|
331
|
-
handleRetry = () => {
|
|
332
|
-
this.setState({ hasError: false, error: null });
|
|
333
|
-
};
|
|
334
|
-
render() {
|
|
335
|
-
if (this.state.hasError) {
|
|
336
|
-
if (this.props.fallback) {
|
|
337
|
-
return this.props.fallback;
|
|
338
|
-
}
|
|
339
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "p-4 border border-dashed border-red-500/50 rounded-lg bg-red-50 dark:bg-red-500/5 text-center", children: [
|
|
340
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className: "text-red-500 font-medium mb-1", children: "\u63D2\u4EF6\u6E32\u67D3\u5931\u8D25" }),
|
|
341
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: "text-xs text-slate-500 dark:text-slate-400 mb-3", children: [
|
|
342
|
-
"\u63D2\u4EF6 ",
|
|
343
|
-
this.props.pluginId ? `"${this.props.pluginId}"` : "\u672A\u77E5",
|
|
344
|
-
" \u53D1\u751F\u5F02\u5E38: ",
|
|
345
|
-
this.state.error?.message
|
|
346
|
-
] }),
|
|
347
|
-
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
348
|
-
"button",
|
|
349
|
-
{
|
|
350
|
-
onClick: this.handleRetry,
|
|
351
|
-
className: "px-3 py-1.5 bg-red-500 hover:bg-red-600 text-white text-xs rounded transition-colors duration-150",
|
|
352
|
-
children: "\u91CD\u8BD5"
|
|
353
|
-
}
|
|
354
|
-
)
|
|
355
|
-
] });
|
|
356
|
-
}
|
|
357
|
-
return this.props.children;
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// src/components/PluginSlot.tsx
|
|
362
|
-
var import_react3 = require("react");
|
|
363
|
-
|
|
364
|
-
// src/adapters/local-storage-adapter.ts
|
|
365
|
-
var LocalStorageAdapter = class {
|
|
366
|
-
prefix;
|
|
367
|
-
constructor(prefix = "") {
|
|
368
|
-
this.prefix = prefix;
|
|
369
|
-
}
|
|
370
|
-
getKey(key) {
|
|
371
|
-
return this.prefix ? `${this.prefix}:${key}` : key;
|
|
372
|
-
}
|
|
373
|
-
getOriginalKey(namespacedKey) {
|
|
374
|
-
if (!this.prefix) return namespacedKey;
|
|
375
|
-
if (namespacedKey.startsWith(this.prefix + ":")) {
|
|
376
|
-
return namespacedKey.slice(this.prefix.length + 1);
|
|
377
|
-
}
|
|
378
|
-
return null;
|
|
379
|
-
}
|
|
380
|
-
getItem(key) {
|
|
381
|
-
return localStorage.getItem(this.getKey(key));
|
|
382
|
-
}
|
|
383
|
-
setItem(key, value) {
|
|
384
|
-
localStorage.setItem(this.getKey(key), value);
|
|
385
|
-
}
|
|
386
|
-
removeItem(key) {
|
|
387
|
-
localStorage.removeItem(this.getKey(key));
|
|
388
|
-
}
|
|
389
|
-
clear() {
|
|
390
|
-
if (!this.prefix) {
|
|
391
|
-
localStorage.clear();
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
const keysToRemove = [];
|
|
395
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
396
|
-
const key = localStorage.key(i);
|
|
397
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
398
|
-
keysToRemove.push(key);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
keysToRemove.forEach((key) => localStorage.removeItem(key));
|
|
402
|
-
}
|
|
403
|
-
get length() {
|
|
404
|
-
if (!this.prefix) return localStorage.length;
|
|
405
|
-
let count = 0;
|
|
406
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
407
|
-
const key = localStorage.key(i);
|
|
408
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
409
|
-
count++;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return count;
|
|
413
|
-
}
|
|
414
|
-
key(index) {
|
|
415
|
-
if (!this.prefix) return localStorage.key(index);
|
|
416
|
-
let count = 0;
|
|
417
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
418
|
-
const key = localStorage.key(i);
|
|
419
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
420
|
-
if (count === index) {
|
|
421
|
-
return this.getOriginalKey(key);
|
|
422
|
-
}
|
|
423
|
-
count++;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
return null;
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
// src/domain/plugin-manager.ts
|
|
431
|
-
init_config_manager();
|
|
432
|
-
|
|
433
|
-
// src/event-bus.ts
|
|
434
|
-
var logger4 = createLogger("EventBus");
|
|
435
|
-
var DefaultEventBus = class {
|
|
436
|
-
listeners = /* @__PURE__ */ new Map();
|
|
437
|
-
on(event, callback) {
|
|
438
|
-
if (!this.listeners.has(event)) {
|
|
439
|
-
this.listeners.set(event, []);
|
|
440
|
-
}
|
|
441
|
-
this.listeners.get(event)?.push(callback);
|
|
442
|
-
return () => this.off(event, callback);
|
|
443
|
-
}
|
|
444
|
-
off(event, callback) {
|
|
445
|
-
const callbacks = this.listeners.get(event);
|
|
446
|
-
if (callbacks) {
|
|
447
|
-
this.listeners.set(event, callbacks.filter((cb) => cb !== callback));
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
emit(event, ...args) {
|
|
451
|
-
const callbacks = this.listeners.get(event);
|
|
452
|
-
if (callbacks) {
|
|
453
|
-
callbacks.forEach((cb) => {
|
|
454
|
-
try {
|
|
455
|
-
cb(...args);
|
|
456
|
-
} catch (error) {
|
|
457
|
-
logger4.error(`\u4E8B\u4EF6\u76D1\u542C\u5668\u5904\u7406\u9519\u8BEF (${event}):`, error);
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
once(event, callback) {
|
|
463
|
-
const wrapper = (...args) => {
|
|
464
|
-
callback(...args);
|
|
465
|
-
this.off(event, wrapper);
|
|
466
|
-
};
|
|
467
|
-
this.on(event, wrapper);
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
// src/sandbox/proxy-sandbox.ts
|
|
472
|
-
var logger5 = createLogger("ProxySandbox");
|
|
473
|
-
var ProxySandbox = class {
|
|
474
|
-
/** 沙箱名称 */
|
|
475
|
-
name;
|
|
476
|
-
/** 代理后的 Window 对象 */
|
|
477
|
-
proxy;
|
|
478
|
-
/** 沙箱是否激活 */
|
|
479
|
-
running = false;
|
|
480
|
-
/** 记录新增/修改的全局变量 */
|
|
481
|
-
updatedValueSet = /* @__PURE__ */ new Set();
|
|
482
|
-
/** 副作用记录池 */
|
|
483
|
-
effectPool = {
|
|
484
|
-
timeouts: /* @__PURE__ */ new Set(),
|
|
485
|
-
intervals: /* @__PURE__ */ new Set(),
|
|
486
|
-
listeners: /* @__PURE__ */ new Map()
|
|
487
|
-
};
|
|
488
|
-
/** 真实的 Window 对象 */
|
|
489
|
-
globalContext;
|
|
490
|
-
/** 白名单全局变量(允许透传访问真实 Window) */
|
|
491
|
-
static globalWhitelist = [
|
|
492
|
-
"System",
|
|
493
|
-
"console",
|
|
494
|
-
// 'setTimeout', // 移除白名单,改为劫持
|
|
495
|
-
// 'setInterval',
|
|
496
|
-
// 'clearTimeout',
|
|
497
|
-
// 'clearInterval',
|
|
498
|
-
"requestAnimationFrame",
|
|
499
|
-
"cancelAnimationFrame",
|
|
500
|
-
// 'addEventListener', // 移除白名单,改为劫持
|
|
501
|
-
// 'removeEventListener',
|
|
502
|
-
"location",
|
|
503
|
-
"history",
|
|
504
|
-
"navigator",
|
|
505
|
-
"document"
|
|
506
|
-
];
|
|
507
|
-
constructor(name, globalContext = window) {
|
|
508
|
-
this.name = name;
|
|
509
|
-
this.globalContext = globalContext;
|
|
510
|
-
const { fakeWindow, propertiesWithGetter } = this.createFakeWindow(globalContext);
|
|
511
|
-
this.patchGlobalEffects(fakeWindow);
|
|
512
|
-
const proxy = new Proxy(fakeWindow, {
|
|
513
|
-
set: (target, p, value) => {
|
|
514
|
-
if (this.running) {
|
|
515
|
-
this.updatedValueSet.add(p);
|
|
516
|
-
target[p] = value;
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
logger5.warn(`${name} \u672A\u8FD0\u884C\uFF0C\u65E0\u6CD5\u8BBE\u7F6E\u5C5E\u6027 '${String(p)}'`);
|
|
520
|
-
return false;
|
|
521
|
-
},
|
|
522
|
-
get: (target, p) => {
|
|
523
|
-
if (p === Symbol.unscopables) return void 0;
|
|
524
|
-
if (p === "window" || p === "self" || p === "globalThis") {
|
|
525
|
-
return this.proxy;
|
|
526
|
-
}
|
|
527
|
-
if (p === "top" || p === "parent") {
|
|
528
|
-
return this.globalContext[p];
|
|
529
|
-
}
|
|
530
|
-
const value = target[p];
|
|
531
|
-
if (value !== void 0 || this.updatedValueSet.has(p)) {
|
|
532
|
-
return value;
|
|
533
|
-
}
|
|
534
|
-
const rawValue = this.globalContext[p];
|
|
535
|
-
if (typeof rawValue === "function" && !this.isConstructor(rawValue)) {
|
|
536
|
-
if (this.isNativeFunction(rawValue)) {
|
|
537
|
-
return rawValue.bind(this.globalContext);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return rawValue;
|
|
541
|
-
},
|
|
542
|
-
has: (target, p) => {
|
|
543
|
-
return p in target || p in this.globalContext;
|
|
544
|
-
},
|
|
545
|
-
defineProperty: (target, p, attributes) => {
|
|
546
|
-
if (this.running) {
|
|
547
|
-
this.updatedValueSet.add(p);
|
|
548
|
-
return Reflect.defineProperty(target, p, attributes);
|
|
549
|
-
}
|
|
550
|
-
return false;
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
this.proxy = proxy;
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* 激活沙箱
|
|
557
|
-
*/
|
|
558
|
-
active() {
|
|
559
|
-
if (!this.running) {
|
|
560
|
-
this.running = true;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* 销毁沙箱
|
|
565
|
-
*/
|
|
566
|
-
inactive() {
|
|
567
|
-
this.running = false;
|
|
568
|
-
this.effectPool.timeouts.forEach((id) => this.globalContext.clearTimeout(id));
|
|
569
|
-
this.effectPool.timeouts.clear();
|
|
570
|
-
this.effectPool.intervals.forEach((id) => this.globalContext.clearInterval(id));
|
|
571
|
-
this.effectPool.intervals.clear();
|
|
572
|
-
this.effectPool.listeners.forEach((listeners, type) => {
|
|
573
|
-
listeners.forEach(({ listener, options }) => {
|
|
574
|
-
this.globalContext.removeEventListener(type, listener, options);
|
|
575
|
-
});
|
|
576
|
-
});
|
|
577
|
-
this.effectPool.listeners.clear();
|
|
578
|
-
logger5.debug(`${this.name} \u5DF2\u505C\u7528\uFF0C\u526F\u4F5C\u7528\u5DF2\u6E05\u9664\u3002`);
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* 在沙箱中执行代码
|
|
582
|
-
* @param code JS 代码字符串
|
|
583
|
-
* @returns 执行结果
|
|
584
|
-
*/
|
|
585
|
-
eval(code) {
|
|
586
|
-
const evalCode = `
|
|
587
|
-
;(function(window, self, globalThis){
|
|
588
|
-
with(window) {
|
|
589
|
-
${code}
|
|
590
|
-
}
|
|
591
|
-
}).bind(window.proxy)(window.proxy, window.proxy, window.proxy);
|
|
592
|
-
`;
|
|
593
|
-
return (0, eval)(evalCode);
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* 创建伪造的 Window 对象
|
|
597
|
-
*/
|
|
598
|
-
createFakeWindow(globalContext) {
|
|
599
|
-
const propertiesWithGetter = /* @__PURE__ */ new Map();
|
|
600
|
-
const fakeWindow = {};
|
|
601
|
-
Object.getOwnPropertyNames(globalContext).forEach((p) => {
|
|
602
|
-
const descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
|
|
603
|
-
if (descriptor && !descriptor.configurable) {
|
|
604
|
-
}
|
|
605
|
-
});
|
|
606
|
-
return { fakeWindow, propertiesWithGetter };
|
|
607
|
-
}
|
|
608
|
-
isConstructor(fn) {
|
|
609
|
-
const prototype = fn.prototype;
|
|
610
|
-
return !!(prototype && prototype.constructor === fn && Object.getOwnPropertyNames(prototype).length > 0);
|
|
611
|
-
}
|
|
612
|
-
isNativeFunction(fn) {
|
|
613
|
-
return fn.toString().indexOf("[native code]") > -1;
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* 劫持全局副作用 API
|
|
617
|
-
*/
|
|
618
|
-
patchGlobalEffects(fakeWindow) {
|
|
619
|
-
fakeWindow.setTimeout = (handler, timeout, ...args) => {
|
|
620
|
-
const id = this.globalContext.setTimeout(handler, timeout, ...args);
|
|
621
|
-
this.effectPool.timeouts.add(id);
|
|
622
|
-
return id;
|
|
623
|
-
};
|
|
624
|
-
fakeWindow.clearTimeout = (id) => {
|
|
625
|
-
if (id) {
|
|
626
|
-
this.effectPool.timeouts.delete(id);
|
|
627
|
-
this.globalContext.clearTimeout(id);
|
|
628
|
-
}
|
|
629
|
-
};
|
|
630
|
-
fakeWindow.setInterval = (handler, timeout, ...args) => {
|
|
631
|
-
const id = this.globalContext.setInterval(handler, timeout, ...args);
|
|
632
|
-
this.effectPool.intervals.add(id);
|
|
633
|
-
return id;
|
|
634
|
-
};
|
|
635
|
-
fakeWindow.clearInterval = (id) => {
|
|
636
|
-
if (id) {
|
|
637
|
-
this.effectPool.intervals.delete(id);
|
|
638
|
-
this.globalContext.clearInterval(id);
|
|
639
|
-
}
|
|
640
|
-
};
|
|
641
|
-
fakeWindow.addEventListener = (type, listener, options) => {
|
|
642
|
-
const listeners = this.effectPool.listeners.get(type) || [];
|
|
643
|
-
listeners.push({ listener, options });
|
|
644
|
-
this.effectPool.listeners.set(type, listeners);
|
|
645
|
-
return this.globalContext.addEventListener(type, listener, options);
|
|
646
|
-
};
|
|
647
|
-
fakeWindow.removeEventListener = (type, listener, options) => {
|
|
648
|
-
const listeners = this.effectPool.listeners.get(type);
|
|
649
|
-
if (listeners) {
|
|
650
|
-
const index = listeners.findIndex((item) => item.listener === listener && item.options === options);
|
|
651
|
-
if (index !== -1) listeners.splice(index, 1);
|
|
652
|
-
}
|
|
653
|
-
return this.globalContext.removeEventListener(type, listener, options);
|
|
654
|
-
};
|
|
655
|
-
}
|
|
656
|
-
};
|
|
657
|
-
|
|
658
|
-
// src/domain/plugin-sandbox.ts
|
|
659
|
-
var PluginSandbox = class {
|
|
660
|
-
pluginId;
|
|
661
|
-
storageManager;
|
|
662
|
-
constructor(pluginId, storageManager) {
|
|
663
|
-
this.pluginId = pluginId;
|
|
664
|
-
this.storageManager = storageManager;
|
|
665
|
-
}
|
|
666
|
-
/**
|
|
667
|
-
* 获取隔离的存储接口
|
|
668
|
-
*/
|
|
669
|
-
get storage() {
|
|
670
|
-
return this.storageManager.getContextStorage(this.pluginId);
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* 获取隔离的日志接口
|
|
674
|
-
*/
|
|
675
|
-
get logger() {
|
|
676
|
-
return createLogger(`Plugin:${this.pluginId}`);
|
|
677
|
-
}
|
|
678
|
-
};
|
|
679
|
-
|
|
680
|
-
// src/domain/plugin-runtime.ts
|
|
681
|
-
var logger6 = createLogger("PluginRuntime");
|
|
682
|
-
var PluginRuntime = class {
|
|
683
|
-
plugin;
|
|
684
|
-
context;
|
|
685
|
-
storageSandbox;
|
|
686
|
-
windowSandbox;
|
|
687
|
-
isLoaded = false;
|
|
688
|
-
isMounted = false;
|
|
689
|
-
constructor(plugin, sharedContext, storageManager) {
|
|
690
|
-
this.plugin = plugin;
|
|
691
|
-
this.storageSandbox = new PluginSandbox(plugin.id, storageManager);
|
|
692
|
-
this.windowSandbox = new ProxySandbox(plugin.id);
|
|
693
|
-
this.context = {
|
|
694
|
-
pluginId: plugin.id,
|
|
695
|
-
api: sharedContext.api,
|
|
696
|
-
events: sharedContext.events,
|
|
697
|
-
storage: this.storageSandbox.storage,
|
|
698
|
-
logger: this.storageSandbox.logger,
|
|
699
|
-
window: this.windowSandbox.proxy,
|
|
700
|
-
getService: (name) => serviceRegistry.get(name),
|
|
701
|
-
registerService: (name, service) => serviceRegistry.register(`${plugin.id}.${name}`, service)
|
|
702
|
-
};
|
|
703
|
-
}
|
|
704
|
-
/**
|
|
705
|
-
* 加载插件 (onLoad)
|
|
706
|
-
* @description 注册服务、初始化非 UI 逻辑
|
|
707
|
-
*/
|
|
708
|
-
async load() {
|
|
709
|
-
if (this.isLoaded) return;
|
|
710
|
-
logger6.debug(`\u6B63\u5728\u52A0\u8F7D\u63D2\u4EF6: ${this.plugin.id}`);
|
|
711
|
-
try {
|
|
712
|
-
if (this.plugin.metadata.api && this.context.api && typeof this.context.api.register === "function") {
|
|
713
|
-
this.context.api.register(this.plugin.metadata.api);
|
|
714
|
-
logger6.debug(`\u5DF2\u4E3A\u63D2\u4EF6 ${this.plugin.id} \u81EA\u52A8\u6CE8\u518C API \u914D\u7F6E`);
|
|
715
|
-
}
|
|
716
|
-
if (this.plugin.onLoad) {
|
|
717
|
-
await this.plugin.onLoad(this.context);
|
|
718
|
-
}
|
|
719
|
-
this.isLoaded = true;
|
|
720
|
-
logger6.info(`\u63D2\u4EF6 ${this.plugin.id} \u5DF2\u52A0\u8F7D\u3002`);
|
|
721
|
-
} catch (error) {
|
|
722
|
-
logger6.error(`\u63D2\u4EF6 ${this.plugin.id} \u52A0\u8F7D\u5931\u8D25:`, error);
|
|
723
|
-
throw error;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
/**
|
|
727
|
-
* 挂载插件 (onMount)
|
|
728
|
-
* @description 激活沙箱、处理 UI 相关的副作用
|
|
729
|
-
*/
|
|
730
|
-
async mount() {
|
|
731
|
-
if (!this.isLoaded) await this.load();
|
|
732
|
-
if (this.isMounted) return;
|
|
733
|
-
logger6.debug(`\u6B63\u5728\u6302\u8F7D\u63D2\u4EF6: ${this.plugin.id}`);
|
|
734
|
-
try {
|
|
735
|
-
this.windowSandbox.active();
|
|
736
|
-
if (this.plugin.onMount) {
|
|
737
|
-
this.plugin.onMount(this.context);
|
|
738
|
-
}
|
|
739
|
-
this.isMounted = true;
|
|
740
|
-
logger6.info(`\u63D2\u4EF6 ${this.plugin.id} \u5DF2\u6302\u8F7D\u3002`);
|
|
741
|
-
} catch (error) {
|
|
742
|
-
logger6.error(`\u63D2\u4EF6 ${this.plugin.id} \u6302\u8F7D\u5931\u8D25:`, error);
|
|
743
|
-
throw error;
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* 卸载插件 (onUnmount)
|
|
748
|
-
*/
|
|
749
|
-
async unmount() {
|
|
750
|
-
if (!this.isMounted) return;
|
|
751
|
-
logger6.debug(`\u6B63\u5728\u5378\u8F7D\u63D2\u4EF6: ${this.plugin.id}`);
|
|
752
|
-
try {
|
|
753
|
-
if (this.plugin.onUnmount) {
|
|
754
|
-
this.plugin.onUnmount(this.context);
|
|
755
|
-
}
|
|
756
|
-
} catch (error) {
|
|
757
|
-
logger6.error(`\u63D2\u4EF6 ${this.plugin.id} \u5378\u8F7D\u65F6\u51FA\u9519:`, error);
|
|
758
|
-
} finally {
|
|
759
|
-
this.windowSandbox.inactive();
|
|
760
|
-
this.isMounted = false;
|
|
761
|
-
logger6.info(`\u63D2\u4EF6 ${this.plugin.id} \u5DF2\u5378\u8F7D\u3002`);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
|
-
* 销毁插件 (完全移除)
|
|
766
|
-
*/
|
|
767
|
-
async destroy() {
|
|
768
|
-
await this.unmount();
|
|
769
|
-
this.isLoaded = false;
|
|
770
|
-
}
|
|
771
|
-
get status() {
|
|
772
|
-
if (this.isMounted) return "mounted";
|
|
773
|
-
if (this.isLoaded) return "loaded";
|
|
774
|
-
return "initial";
|
|
775
|
-
}
|
|
776
|
-
};
|
|
777
|
-
|
|
778
|
-
// src/adapters/scoped-storage-adapter.ts
|
|
779
|
-
var ScopedStorageAdapter = class {
|
|
780
|
-
constructor(underlyingStorage, prefix) {
|
|
781
|
-
this.underlyingStorage = underlyingStorage;
|
|
782
|
-
this.prefix = prefix;
|
|
783
|
-
}
|
|
784
|
-
getKey(key) {
|
|
785
|
-
return this.prefix ? `${this.prefix}:${key}` : key;
|
|
786
|
-
}
|
|
787
|
-
getOriginalKey(namespacedKey) {
|
|
788
|
-
if (!this.prefix) return namespacedKey;
|
|
789
|
-
if (namespacedKey.startsWith(this.prefix + ":")) {
|
|
790
|
-
return namespacedKey.slice(this.prefix.length + 1);
|
|
791
|
-
}
|
|
792
|
-
return null;
|
|
793
|
-
}
|
|
794
|
-
getItem(key) {
|
|
795
|
-
return this.underlyingStorage.getItem(this.getKey(key));
|
|
796
|
-
}
|
|
797
|
-
setItem(key, value) {
|
|
798
|
-
this.underlyingStorage.setItem(this.getKey(key), value);
|
|
799
|
-
}
|
|
800
|
-
removeItem(key) {
|
|
801
|
-
this.underlyingStorage.removeItem(this.getKey(key));
|
|
802
|
-
}
|
|
803
|
-
clear() {
|
|
804
|
-
if (!this.prefix) {
|
|
805
|
-
this.underlyingStorage.clear();
|
|
806
|
-
return;
|
|
807
|
-
}
|
|
808
|
-
const keysToRemove = [];
|
|
809
|
-
for (let i = 0; i < this.underlyingStorage.length; i++) {
|
|
810
|
-
const key = this.underlyingStorage.key(i);
|
|
811
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
812
|
-
keysToRemove.push(key);
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
keysToRemove.forEach((k) => this.underlyingStorage.removeItem(k));
|
|
816
|
-
}
|
|
817
|
-
get length() {
|
|
818
|
-
let count = 0;
|
|
819
|
-
for (let i = 0; i < this.underlyingStorage.length; i++) {
|
|
820
|
-
const key = this.underlyingStorage.key(i);
|
|
821
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
822
|
-
count++;
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
return count;
|
|
826
|
-
}
|
|
827
|
-
key(index) {
|
|
828
|
-
let count = 0;
|
|
829
|
-
for (let i = 0; i < this.underlyingStorage.length; i++) {
|
|
830
|
-
const key = this.underlyingStorage.key(i);
|
|
831
|
-
if (key && key.startsWith(this.prefix + ":")) {
|
|
832
|
-
if (count === index) {
|
|
833
|
-
return this.getOriginalKey(key);
|
|
834
|
-
}
|
|
835
|
-
count++;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
return null;
|
|
839
|
-
}
|
|
840
|
-
};
|
|
841
|
-
|
|
842
|
-
// src/domain/storage-manager.ts
|
|
843
|
-
var StorageManager = class {
|
|
844
|
-
baseStorage;
|
|
845
|
-
schemas = /* @__PURE__ */ new Map();
|
|
846
|
-
constructor(baseStorage) {
|
|
847
|
-
this.baseStorage = baseStorage;
|
|
848
|
-
}
|
|
849
|
-
/**
|
|
850
|
-
* 注册插件存储 Schema
|
|
851
|
-
*/
|
|
852
|
-
registerSchema(pluginId, schema) {
|
|
853
|
-
this.schemas.set(pluginId, schema);
|
|
854
|
-
}
|
|
855
|
-
/**
|
|
856
|
-
* 验证 Key 是否符合 Schema 定义 (仅在开发环境或严格模式下警告)
|
|
857
|
-
*/
|
|
858
|
-
validateKey(pluginId, key, scope = "plugin") {
|
|
859
|
-
const schemaList = this.schemas.get(pluginId);
|
|
860
|
-
if (!schemaList) return;
|
|
861
|
-
const item = schemaList.find((s) => s.key === key);
|
|
862
|
-
if (!item) {
|
|
863
|
-
console.warn(`[Storage] Key "${key}" not defined in plugin "${pluginId}" schema.`);
|
|
864
|
-
return;
|
|
865
|
-
}
|
|
866
|
-
const definedScope = item.scope || "plugin";
|
|
867
|
-
if (definedScope !== scope) {
|
|
868
|
-
console.warn(`[Storage] Key "${key}" defined in scope "${definedScope}" but accessed via "${scope}".`);
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* 获取插件专用存储适配器
|
|
873
|
-
* @param pluginId 插件 ID
|
|
874
|
-
*/
|
|
875
|
-
getPluginStorage(pluginId) {
|
|
876
|
-
return new ScopedStorageAdapter(this.baseStorage, `plugin:${pluginId}`);
|
|
877
|
-
}
|
|
878
|
-
/**
|
|
879
|
-
* 获取共享存储适配器
|
|
880
|
-
*/
|
|
881
|
-
getSharedStorage() {
|
|
882
|
-
return new ScopedStorageAdapter(this.baseStorage, "shared");
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* 获取系统存储适配器
|
|
886
|
-
*/
|
|
887
|
-
getSystemStorage() {
|
|
888
|
-
return new ScopedStorageAdapter(this.baseStorage, "system");
|
|
889
|
-
}
|
|
890
|
-
/**
|
|
891
|
-
* 获取插件上下文中的 storage 对象 (包含类型转换辅助方法)
|
|
892
|
-
* @param pluginId 插件 ID
|
|
893
|
-
*/
|
|
894
|
-
getContextStorage(pluginId) {
|
|
895
|
-
const pluginStorage = this.getPluginStorage(pluginId);
|
|
896
|
-
const sharedStorage = this.getSharedStorage();
|
|
897
|
-
const createHelpers = (adapter, scope) => ({
|
|
898
|
-
get: (key) => {
|
|
899
|
-
this.validateKey(pluginId, key, scope);
|
|
900
|
-
try {
|
|
901
|
-
const val = adapter.getItem(key);
|
|
902
|
-
if (val === null) {
|
|
903
|
-
const schema = this.schemas.get(pluginId)?.find((s) => s.key === key);
|
|
904
|
-
if (schema && schema.default !== void 0) {
|
|
905
|
-
return schema.default;
|
|
906
|
-
}
|
|
907
|
-
return null;
|
|
908
|
-
}
|
|
909
|
-
try {
|
|
910
|
-
return JSON.parse(val);
|
|
911
|
-
} catch {
|
|
912
|
-
return val;
|
|
913
|
-
}
|
|
914
|
-
} catch (e) {
|
|
915
|
-
console.warn(`[Storage] Failed to read key "${key}"`, e);
|
|
916
|
-
return null;
|
|
917
|
-
}
|
|
918
|
-
},
|
|
919
|
-
set: (key, value) => {
|
|
920
|
-
this.validateKey(pluginId, key, scope);
|
|
921
|
-
try {
|
|
922
|
-
adapter.setItem(key, JSON.stringify(value));
|
|
923
|
-
} catch (e) {
|
|
924
|
-
console.warn(`[Storage] Failed to stringify key "${key}"`, e);
|
|
925
|
-
}
|
|
926
|
-
},
|
|
927
|
-
remove: (key) => {
|
|
928
|
-
this.validateKey(pluginId, key, scope);
|
|
929
|
-
adapter.removeItem(key);
|
|
930
|
-
}
|
|
931
|
-
});
|
|
932
|
-
return {
|
|
933
|
-
...createHelpers(pluginStorage, "plugin"),
|
|
934
|
-
shared: createHelpers(sharedStorage, "shared")
|
|
935
|
-
};
|
|
936
|
-
}
|
|
937
|
-
};
|
|
938
|
-
|
|
939
|
-
// src/domain/plugin-manager.ts
|
|
940
|
-
var logger7 = createLogger("PluginManager");
|
|
941
|
-
var PluginManager = class {
|
|
942
|
-
/** 全局事件总线 */
|
|
943
|
-
eventBus = new DefaultEventBus();
|
|
944
|
-
/** 存储接口 */
|
|
945
|
-
storageManager;
|
|
946
|
-
/** 所有插件运行时实例 */
|
|
947
|
-
runtimes = /* @__PURE__ */ new Map();
|
|
948
|
-
/** 所有注册的插件(原始数据) */
|
|
949
|
-
plugins = /* @__PURE__ */ new Map();
|
|
950
|
-
/** 收集的路由 */
|
|
951
|
-
routes = [];
|
|
952
|
-
/** 收集的扩展点 */
|
|
953
|
-
extensions = /* @__PURE__ */ new Map();
|
|
954
|
-
/** 插件状态管理(启用/禁用、排序) */
|
|
955
|
-
pluginStates = {};
|
|
956
|
-
/** 状态变更监听器 */
|
|
957
|
-
listeners = /* @__PURE__ */ new Set();
|
|
958
|
-
/** 共享上下文缓存 */
|
|
959
|
-
sharedContext = null;
|
|
960
|
-
/** 收集的工具函数 */
|
|
961
|
-
utils = {};
|
|
962
|
-
constructor(storage) {
|
|
963
|
-
this.storageManager = new StorageManager(storage);
|
|
964
|
-
this.loadStates();
|
|
965
|
-
this.subscribe(() => {
|
|
966
|
-
this.saveStates();
|
|
967
|
-
});
|
|
968
|
-
}
|
|
969
|
-
loadStates() {
|
|
970
|
-
try {
|
|
971
|
-
const systemStorage = this.storageManager.getSystemStorage();
|
|
972
|
-
const savedStates = systemStorage.getItem("plugin_states");
|
|
973
|
-
if (savedStates) {
|
|
974
|
-
this.pluginStates = JSON.parse(savedStates);
|
|
975
|
-
logger7.debug("\u4ECE\u7CFB\u7EDF\u5B58\u50A8\u4E2D\u52A0\u8F7D\u72B6\u6001:", this.pluginStates);
|
|
976
|
-
}
|
|
977
|
-
} catch (e) {
|
|
978
|
-
logger7.error("\u52A0\u8F7D\u63D2\u4EF6\u72B6\u6001\u5931\u8D25:", e);
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
saveStates() {
|
|
982
|
-
try {
|
|
983
|
-
this.storageManager.getSystemStorage().setItem("plugin_states", JSON.stringify(this.pluginStates));
|
|
984
|
-
logger7.debug("\u5DF2\u4FDD\u5B58\u63D2\u4EF6\u72B6\u6001\u5230\u5B58\u50A8");
|
|
985
|
-
} catch (e) {
|
|
986
|
-
logger7.error("\u4FDD\u5B58\u63D2\u4EF6\u72B6\u6001\u5931\u8D25:", e);
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
/**
|
|
990
|
-
* 订阅插件状态变更
|
|
991
|
-
* @param listener 监听函数
|
|
992
|
-
* @returns 取消订阅函数
|
|
993
|
-
*/
|
|
994
|
-
subscribe(listener) {
|
|
995
|
-
this.listeners.add(listener);
|
|
996
|
-
return () => {
|
|
997
|
-
this.listeners.delete(listener);
|
|
998
|
-
};
|
|
999
|
-
}
|
|
1000
|
-
getStorageManager() {
|
|
1001
|
-
return this.storageManager;
|
|
1002
|
-
}
|
|
1003
|
-
/**
|
|
1004
|
-
* 触发状态变更通知
|
|
1005
|
-
*/
|
|
1006
|
-
notify() {
|
|
1007
|
-
this.listeners.forEach((listener) => listener());
|
|
1008
|
-
}
|
|
1009
|
-
/**
|
|
1010
|
-
* 获取所有已注册的插件
|
|
1011
|
-
*/
|
|
1012
|
-
getPlugins() {
|
|
1013
|
-
const typePriority = {
|
|
1014
|
-
system: 0,
|
|
1015
|
-
theme: 1,
|
|
1016
|
-
renderer: 2,
|
|
1017
|
-
functional: 3,
|
|
1018
|
-
business: 4,
|
|
1019
|
-
view: 5
|
|
1020
|
-
};
|
|
1021
|
-
return Array.from(this.plugins.values()).sort((a, b) => {
|
|
1022
|
-
const priorityA = typePriority[a.metadata.type] ?? 99;
|
|
1023
|
-
const priorityB = typePriority[b.metadata.type] ?? 99;
|
|
1024
|
-
if (priorityA !== priorityB) {
|
|
1025
|
-
return priorityA - priorityB;
|
|
1026
|
-
}
|
|
1027
|
-
const stateA = this.pluginStates[a.id] || { order: 0 };
|
|
1028
|
-
const stateB = this.pluginStates[b.id] || { order: 0 };
|
|
1029
|
-
return stateA.order - stateB.order;
|
|
1030
|
-
});
|
|
1031
|
-
}
|
|
1032
|
-
/**
|
|
1033
|
-
* 获取插件状态
|
|
1034
|
-
* @param pluginId 插件 ID
|
|
1035
|
-
*/
|
|
1036
|
-
getPluginState(pluginId) {
|
|
1037
|
-
return this.pluginStates[pluginId] || { enabled: true, order: 0 };
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* 检查插件是否启用
|
|
1041
|
-
* @param pluginId 插件 ID
|
|
1042
|
-
*/
|
|
1043
|
-
isPluginEnabled(pluginId) {
|
|
1044
|
-
const state = this.pluginStates[pluginId];
|
|
1045
|
-
return state ? state.enabled : true;
|
|
1046
|
-
}
|
|
1047
|
-
/**
|
|
1048
|
-
* 切换插件启用状态
|
|
1049
|
-
* @param pluginId 插件 ID
|
|
1050
|
-
* @param enabled 是否启用
|
|
1051
|
-
*/
|
|
1052
|
-
togglePlugin(pluginId, enabled) {
|
|
1053
|
-
const state = this.pluginStates[pluginId] || { enabled: true, order: 0 };
|
|
1054
|
-
this.pluginStates[pluginId] = { ...state, enabled };
|
|
1055
|
-
if (!enabled) {
|
|
1056
|
-
const runtime = this.runtimes.get(pluginId);
|
|
1057
|
-
if (runtime) {
|
|
1058
|
-
runtime.unmount();
|
|
1059
|
-
this.runtimes.delete(pluginId);
|
|
1060
|
-
}
|
|
1061
|
-
} else {
|
|
1062
|
-
if (this.sharedContext) {
|
|
1063
|
-
const plugin = this.plugins.get(pluginId);
|
|
1064
|
-
if (plugin) {
|
|
1065
|
-
try {
|
|
1066
|
-
const runtime = new PluginRuntime(plugin, this.sharedContext, this.storageManager);
|
|
1067
|
-
this.runtimes.set(pluginId, runtime);
|
|
1068
|
-
runtime.mount();
|
|
1069
|
-
} catch (e) {
|
|
1070
|
-
logger7.error(`\u542F\u7528\u63D2\u4EF6 ${pluginId} \u5931\u8D25:`, e);
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
this.notify();
|
|
1076
|
-
}
|
|
1077
|
-
/**
|
|
1078
|
-
* 设置插件排序
|
|
1079
|
-
* @param pluginId 插件 ID
|
|
1080
|
-
* @param order 排序值
|
|
1081
|
-
*/
|
|
1082
|
-
setPluginOrder(pluginId, order) {
|
|
1083
|
-
const state = this.pluginStates[pluginId] || { enabled: true, order: 0 };
|
|
1084
|
-
this.pluginStates[pluginId] = { ...state, order };
|
|
1085
|
-
this.notify();
|
|
1086
|
-
}
|
|
1087
|
-
/**
|
|
1088
|
-
* 获取插件的统一能力描述
|
|
1089
|
-
* @param pluginId 插件 ID
|
|
1090
|
-
*/
|
|
1091
|
-
getUnifiedCapabilities(pluginId) {
|
|
1092
|
-
const plugin = this.plugins.get(pluginId);
|
|
1093
|
-
return plugin?.metadata.capabilities || {};
|
|
1094
|
-
}
|
|
1095
|
-
/**
|
|
1096
|
-
* 更新插件配置
|
|
1097
|
-
* @param pluginId 插件 ID
|
|
1098
|
-
* @param key 配置项 Key
|
|
1099
|
-
* @param value 配置值
|
|
1100
|
-
*/
|
|
1101
|
-
updatePluginConfig(pluginId, key, value) {
|
|
1102
|
-
const currentConfig = configManager.get(pluginId) || {};
|
|
1103
|
-
currentConfig[key] = value;
|
|
1104
|
-
configManager.set(pluginId, currentConfig);
|
|
1105
|
-
try {
|
|
1106
|
-
this.storageManager.getContextStorage(pluginId).set(key, value);
|
|
1107
|
-
} catch (e) {
|
|
1108
|
-
logger7.warn("\u4FDD\u5B58\u914D\u7F6E\u5230\u5B58\u50A8\u5931\u8D25", e);
|
|
1109
|
-
}
|
|
1110
|
-
this.eventBus.emit("config:changed", { pluginId, key, value });
|
|
1111
|
-
}
|
|
1112
|
-
/**
|
|
1113
|
-
* 获取插件配置
|
|
1114
|
-
* @param pluginId 插件 ID
|
|
1115
|
-
* @param key 配置键
|
|
1116
|
-
* @returns 配置值
|
|
1117
|
-
*/
|
|
1118
|
-
getPluginConfig(pluginId, key) {
|
|
1119
|
-
const config = configManager.get(pluginId);
|
|
1120
|
-
return config ? config[key] : void 0;
|
|
1121
|
-
}
|
|
1122
|
-
/**
|
|
1123
|
-
* 获取系统全局配置 (非插件特定配置)
|
|
1124
|
-
* @param key 配置键 (如 title, version)
|
|
1125
|
-
*/
|
|
1126
|
-
getSystemConfig(key) {
|
|
1127
|
-
const systemConfig = configManager.get("system");
|
|
1128
|
-
return systemConfig ? systemConfig[key] : void 0;
|
|
1129
|
-
}
|
|
1130
|
-
/**
|
|
1131
|
-
* 获取注册的服务
|
|
1132
|
-
* @param name 服务名称
|
|
1133
|
-
*/
|
|
1134
|
-
getService(name) {
|
|
1135
|
-
return serviceRegistry.get(name);
|
|
1136
|
-
}
|
|
1137
|
-
/**
|
|
1138
|
-
* 获取指定插槽的扩展
|
|
1139
|
-
* @param slot 插槽位置
|
|
1140
|
-
*/
|
|
1141
|
-
getExtensions(slot) {
|
|
1142
|
-
const targetSlot = slot;
|
|
1143
|
-
let extensions = this.extensions.get(targetSlot) || [];
|
|
1144
|
-
extensions = extensions.filter((ext) => {
|
|
1145
|
-
const pluginId = ext._pluginId;
|
|
1146
|
-
return !pluginId || this.isPluginEnabled(pluginId);
|
|
1147
|
-
});
|
|
1148
|
-
return extensions.sort((a, b) => (a.order || 0) - (b.order || 0));
|
|
1149
|
-
}
|
|
1150
|
-
/**
|
|
1151
|
-
* 获取所有收集到的路由
|
|
1152
|
-
*/
|
|
1153
|
-
getRoutes() {
|
|
1154
|
-
const activeRoutes = [];
|
|
1155
|
-
this.getPlugins().forEach((plugin) => {
|
|
1156
|
-
if (this.isPluginEnabled(plugin.id) && plugin.metadata.routes) {
|
|
1157
|
-
activeRoutes.push(...plugin.metadata.routes);
|
|
1158
|
-
}
|
|
1159
|
-
});
|
|
1160
|
-
return activeRoutes;
|
|
1161
|
-
}
|
|
1162
|
-
/**
|
|
1163
|
-
* 注册插件
|
|
1164
|
-
* @param plugin 插件实例
|
|
1165
|
-
* @param notify 是否触发状态变更通知
|
|
1166
|
-
*/
|
|
1167
|
-
register(plugin, notify = true) {
|
|
1168
|
-
if (!this.validatePlugin(plugin)) {
|
|
1169
|
-
logger7.error(`\u63D2\u4EF6\u6CE8\u518C\u5931\u8D25: ${plugin?.id || "\u672A\u77E5"}`);
|
|
1170
|
-
return;
|
|
1171
|
-
}
|
|
1172
|
-
if (this.plugins.has(plugin.id)) {
|
|
1173
|
-
return;
|
|
1174
|
-
}
|
|
1175
|
-
if (plugin.metadata.storage) {
|
|
1176
|
-
this.storageManager.registerSchema(plugin.id, plugin.metadata.storage);
|
|
1177
|
-
}
|
|
1178
|
-
if (!this.pluginStates[plugin.id]) {
|
|
1179
|
-
this.pluginStates[plugin.id] = { enabled: true, order: 0 };
|
|
1180
|
-
}
|
|
1181
|
-
const configFromStorage = {};
|
|
1182
|
-
const pluginStorage = this.storageManager.getPluginStorage(plugin.id);
|
|
1183
|
-
if (plugin.metadata.configuration) {
|
|
1184
|
-
plugin.metadata.configuration.forEach((item) => {
|
|
1185
|
-
if (item.default !== void 0) {
|
|
1186
|
-
configFromStorage[item.key] = item.default;
|
|
1187
|
-
}
|
|
1188
|
-
try {
|
|
1189
|
-
const saved = pluginStorage.getItem(item.key);
|
|
1190
|
-
if (saved !== null) {
|
|
1191
|
-
configFromStorage[item.key] = JSON.parse(saved);
|
|
1192
|
-
}
|
|
1193
|
-
} catch (e) {
|
|
1194
|
-
}
|
|
1195
|
-
});
|
|
1196
|
-
}
|
|
1197
|
-
const mergedConfig = {
|
|
1198
|
-
...plugin.defaultConfig,
|
|
1199
|
-
...configFromStorage,
|
|
1200
|
-
...configManager.get(plugin.id) || {}
|
|
1201
|
-
};
|
|
1202
|
-
configManager.set(plugin.id, mergedConfig);
|
|
1203
|
-
if (mergedConfig) {
|
|
1204
|
-
Object.entries(mergedConfig).forEach(([key, value]) => {
|
|
1205
|
-
try {
|
|
1206
|
-
pluginStorage.setItem(key, JSON.stringify(value));
|
|
1207
|
-
} catch (e) {
|
|
1208
|
-
}
|
|
1209
|
-
});
|
|
1210
|
-
}
|
|
1211
|
-
switch (plugin.metadata.type) {
|
|
1212
|
-
case "business":
|
|
1213
|
-
this.handleBusinessPlugin(plugin);
|
|
1214
|
-
break;
|
|
1215
|
-
case "functional":
|
|
1216
|
-
this.handleFunctionalPlugin(plugin);
|
|
1217
|
-
break;
|
|
1218
|
-
case "view":
|
|
1219
|
-
this.handleViewPlugin(plugin);
|
|
1220
|
-
break;
|
|
1221
|
-
case "theme":
|
|
1222
|
-
this.handleThemePlugin(plugin);
|
|
1223
|
-
break;
|
|
1224
|
-
case "system":
|
|
1225
|
-
this.handleSystemPlugin(plugin);
|
|
1226
|
-
break;
|
|
1227
|
-
case "renderer":
|
|
1228
|
-
break;
|
|
1229
|
-
default:
|
|
1230
|
-
logger7.warn(`\u63D2\u4EF6 ${plugin.id} \u7C7B\u578B\u672A\u77E5: ${plugin.metadata.type}`);
|
|
1231
|
-
break;
|
|
1232
|
-
}
|
|
1233
|
-
if (plugin.metadata.routes && plugin.metadata.routes.length > 0) {
|
|
1234
|
-
logger7.info(`\u5DF2\u4ECE\u63D2\u4EF6 ${plugin.id} \u6536\u96C6\u8DEF\u7531:`, plugin.metadata.routes);
|
|
1235
|
-
}
|
|
1236
|
-
if (plugin.metadata.extensions && plugin.metadata.extensions.length > 0) {
|
|
1237
|
-
plugin.metadata.extensions.forEach((ext) => {
|
|
1238
|
-
const list = this.extensions.get(ext.slot) || [];
|
|
1239
|
-
list.push({ ...ext, _pluginId: plugin.id });
|
|
1240
|
-
this.extensions.set(ext.slot, list);
|
|
1241
|
-
});
|
|
1242
|
-
logger7.info(`\u5DF2\u4ECE\u63D2\u4EF6 ${plugin.id} \u6536\u96C6\u6269\u5C55\u70B9`);
|
|
1243
|
-
}
|
|
1244
|
-
this.plugins.set(plugin.id, plugin);
|
|
1245
|
-
logger7.info(`\u63D2\u4EF6 ${plugin.id} \u5DF2\u6CE8\u518C\u4E3A ${plugin.metadata.type}\u3002`);
|
|
1246
|
-
if (notify) {
|
|
1247
|
-
this.notify();
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
/**
|
|
1251
|
-
* 初始化所有插件
|
|
1252
|
-
* @param sharedContext 共享上下文
|
|
1253
|
-
*/
|
|
1254
|
-
async initPlugins(sharedContext = {}) {
|
|
1255
|
-
this.sharedContext = {
|
|
1256
|
-
...sharedContext,
|
|
1257
|
-
events: this.eventBus
|
|
1258
|
-
};
|
|
1259
|
-
this.plugins.forEach((plugin) => {
|
|
1260
|
-
if (!this.isPluginEnabled(plugin.id)) return;
|
|
1261
|
-
if (!this.runtimes.has(plugin.id)) {
|
|
1262
|
-
const runtime = new PluginRuntime(plugin, this.sharedContext, this.storageManager);
|
|
1263
|
-
this.runtimes.set(plugin.id, runtime);
|
|
1264
|
-
}
|
|
1265
|
-
});
|
|
1266
|
-
const sortedPluginIds = this.getSortedPluginIds();
|
|
1267
|
-
for (const id of sortedPluginIds) {
|
|
1268
|
-
const runtime = this.runtimes.get(id);
|
|
1269
|
-
if (runtime) {
|
|
1270
|
-
try {
|
|
1271
|
-
await runtime.load();
|
|
1272
|
-
} catch (e) {
|
|
1273
|
-
logger7.error(`\u63D2\u4EF6 ${id} \u52A0\u8F7D\u5931\u8D25:`, e);
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1277
|
-
for (const id of sortedPluginIds) {
|
|
1278
|
-
const runtime = this.runtimes.get(id);
|
|
1279
|
-
if (runtime) {
|
|
1280
|
-
try {
|
|
1281
|
-
await runtime.mount();
|
|
1282
|
-
} catch (e) {
|
|
1283
|
-
logger7.error(`\u63D2\u4EF6 ${id} \u6302\u8F7D\u5931\u8D25:`, e);
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* 获取排序后的插件 ID 列表 (处理依赖)
|
|
1290
|
-
*/
|
|
1291
|
-
getSortedPluginIds() {
|
|
1292
|
-
const ids = Array.from(this.runtimes.keys());
|
|
1293
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1294
|
-
const sorted = [];
|
|
1295
|
-
const visiting = /* @__PURE__ */ new Set();
|
|
1296
|
-
const visit = (id) => {
|
|
1297
|
-
if (visited.has(id)) return;
|
|
1298
|
-
if (visiting.has(id)) {
|
|
1299
|
-
logger7.error(`\u5FAA\u73AF\u4F9D\u8D56\u68C0\u6D4B\u5230: ${id}`);
|
|
1300
|
-
return;
|
|
1301
|
-
}
|
|
1302
|
-
visiting.add(id);
|
|
1303
|
-
const plugin = this.plugins.get(id);
|
|
1304
|
-
if (plugin?.metadata.dependencies) {
|
|
1305
|
-
plugin.metadata.dependencies.forEach((depId) => {
|
|
1306
|
-
if (this.runtimes.has(depId)) {
|
|
1307
|
-
visit(depId);
|
|
1308
|
-
}
|
|
1309
|
-
});
|
|
1310
|
-
}
|
|
1311
|
-
visiting.delete(id);
|
|
1312
|
-
visited.add(id);
|
|
1313
|
-
sorted.push(id);
|
|
1314
|
-
};
|
|
1315
|
-
const priorityMap = {
|
|
1316
|
-
"system": 100,
|
|
1317
|
-
"functional": 50,
|
|
1318
|
-
"business": 10
|
|
1319
|
-
};
|
|
1320
|
-
const sortedIdsByType = [...ids].sort((a, b) => {
|
|
1321
|
-
const pA = priorityMap[this.plugins.get(a)?.metadata.type || ""] || 0;
|
|
1322
|
-
const pB = priorityMap[this.plugins.get(b)?.metadata.type || ""] || 0;
|
|
1323
|
-
return pB - pA;
|
|
1324
|
-
});
|
|
1325
|
-
sortedIdsByType.forEach((id) => visit(id));
|
|
1326
|
-
return sorted;
|
|
1327
|
-
}
|
|
1328
|
-
/**
|
|
1329
|
-
* 加载插件列表
|
|
1330
|
-
* @param configs 插件配置
|
|
1331
|
-
* @param registry 插件注册表 (动态导入函数)
|
|
1332
|
-
*/
|
|
1333
|
-
async loadPlugins(configs, registry) {
|
|
1334
|
-
logger7.info("\u5F00\u59CB\u52A0\u8F7D\u63D2\u4EF6...");
|
|
1335
|
-
const localLoadPromises = Object.entries(registry).map(async ([registryId, importFn]) => {
|
|
1336
|
-
try {
|
|
1337
|
-
const module2 = await importFn();
|
|
1338
|
-
const config = configs[registryId];
|
|
1339
|
-
const plugin = this.instantiatePlugin(registryId, module2, config);
|
|
1340
|
-
if (plugin && config) {
|
|
1341
|
-
configManager.set(plugin.id, config);
|
|
1342
|
-
}
|
|
1343
|
-
return plugin;
|
|
1344
|
-
} catch (e) {
|
|
1345
|
-
logger7.error(`\u52A0\u8F7D\u672C\u5730\u63D2\u4EF6\u6A21\u5757 ${registryId} \u5931\u8D25:`, e);
|
|
1346
|
-
return null;
|
|
1347
|
-
}
|
|
1348
|
-
});
|
|
1349
|
-
const onlineLoadPromises = Object.entries(configs).filter(([id, config]) => config.url && !registry[id]).map(async ([pluginId, config]) => {
|
|
1350
|
-
try {
|
|
1351
|
-
const plugin = await this.loadRemotePlugin(pluginId, config.url, config);
|
|
1352
|
-
if (plugin && config) {
|
|
1353
|
-
configManager.set(plugin.id, config);
|
|
1354
|
-
}
|
|
1355
|
-
return plugin;
|
|
1356
|
-
} catch (e) {
|
|
1357
|
-
logger7.error(`\u52A0\u8F7D\u5728\u7EBF\u63D2\u4EF6 ${pluginId} \u5931\u8D25:`, e);
|
|
1358
|
-
return null;
|
|
1359
|
-
}
|
|
1360
|
-
});
|
|
1361
|
-
const loadedPlugins = await Promise.all([...localLoadPromises, ...onlineLoadPromises]);
|
|
1362
|
-
loadedPlugins.forEach((plugin) => {
|
|
1363
|
-
if (plugin) {
|
|
1364
|
-
this.register(plugin, false);
|
|
1365
|
-
}
|
|
1366
|
-
});
|
|
1367
|
-
this.notify();
|
|
1368
|
-
logger7.info(`\u63D2\u4EF6\u52A0\u8F7D\u5B8C\u6210\uFF0C\u5171\u52A0\u8F7D ${this.plugins.size} \u4E2A\u63D2\u4EF6`);
|
|
1369
|
-
}
|
|
1370
|
-
/**
|
|
1371
|
-
* 加载远程插件
|
|
1372
|
-
* @param pluginId 插件 ID
|
|
1373
|
-
* @param url 远程 URL
|
|
1374
|
-
* @param config 插件配置
|
|
1375
|
-
*/
|
|
1376
|
-
async loadRemotePlugin(pluginId, url, config) {
|
|
1377
|
-
logger7.info(`\u6B63\u5728\u4ECE ${url} \u52A0\u8F7D\u8FDC\u7A0B\u63D2\u4EF6 ${pluginId}...`);
|
|
1378
|
-
try {
|
|
1379
|
-
const module2 = await import(
|
|
1380
|
-
/* @vite-ignore */
|
|
1381
|
-
url
|
|
1382
|
-
);
|
|
1383
|
-
return this.instantiatePlugin(pluginId, module2, config);
|
|
1384
|
-
} catch (e) {
|
|
1385
|
-
logger7.warn(`ESM \u52A0\u8F7D\u5931\u8D25\uFF0C\u5C1D\u8BD5 IIFE \u52A0\u8F7D: ${pluginId}`);
|
|
1386
|
-
return new Promise((resolve, reject) => {
|
|
1387
|
-
const script = document.createElement("script");
|
|
1388
|
-
script.src = url;
|
|
1389
|
-
script.onload = () => {
|
|
1390
|
-
const globalName = pluginId.replace(/[^a-zA-Z0-9]/g, "_");
|
|
1391
|
-
const pluginModule = window[globalName];
|
|
1392
|
-
if (pluginModule) {
|
|
1393
|
-
resolve(this.instantiatePlugin(pluginId, pluginModule, config));
|
|
1394
|
-
} else {
|
|
1395
|
-
reject(new Error(`\u8FDC\u7A0B\u63D2\u4EF6 ${pluginId} \u52A0\u8F7D\u540E\u672A\u627E\u5230\u5168\u5C40\u53D8\u91CF ${globalName}`));
|
|
1396
|
-
}
|
|
1397
|
-
};
|
|
1398
|
-
script.onerror = () => reject(new Error(`\u8FDC\u7A0B\u63D2\u4EF6 ${pluginId} \u52A0\u8F7D\u5931\u8D25: ${url}`));
|
|
1399
|
-
document.head.appendChild(script);
|
|
1400
|
-
});
|
|
1401
|
-
}
|
|
1402
|
-
}
|
|
1403
|
-
/**
|
|
1404
|
-
* 实例化插件
|
|
1405
|
-
*/
|
|
1406
|
-
instantiatePlugin(pluginId, module2, config) {
|
|
1407
|
-
let PluginClass = module2.default;
|
|
1408
|
-
if (!PluginClass) {
|
|
1409
|
-
const key = Object.keys(module2).find((k) => k.endsWith("Plugin"));
|
|
1410
|
-
if (key) PluginClass = module2[key];
|
|
1411
|
-
}
|
|
1412
|
-
if (!PluginClass && typeof module2 === "object") {
|
|
1413
|
-
if (module2.id && module2.metadata) {
|
|
1414
|
-
PluginClass = module2;
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
if (PluginClass) {
|
|
1418
|
-
const pluginInstance = typeof PluginClass === "function" ? new PluginClass() : PluginClass;
|
|
1419
|
-
if (!pluginInstance.id && pluginInstance.metadata?.id) {
|
|
1420
|
-
pluginInstance.id = pluginInstance.metadata.id;
|
|
1421
|
-
} else if (!pluginInstance.id) {
|
|
1422
|
-
pluginInstance.id = pluginId;
|
|
1423
|
-
}
|
|
1424
|
-
if (config) {
|
|
1425
|
-
pluginInstance.defaultConfig = { ...pluginInstance.defaultConfig, ...config };
|
|
1426
|
-
}
|
|
1427
|
-
return pluginInstance;
|
|
1428
|
-
}
|
|
1429
|
-
logger7.warn(`\u6A21\u5757 ${pluginId} \u672A\u5BFC\u51FA\u6709\u6548\u7684\u63D2\u4EF6\u5165\u53E3`);
|
|
1430
|
-
return null;
|
|
1431
|
-
}
|
|
1432
|
-
// --- Private Handlers ---
|
|
1433
|
-
validatePlugin(plugin) {
|
|
1434
|
-
if (!plugin.id) return false;
|
|
1435
|
-
if (!plugin.metadata) return false;
|
|
1436
|
-
return true;
|
|
1437
|
-
}
|
|
1438
|
-
handleBusinessPlugin(plugin) {
|
|
1439
|
-
}
|
|
1440
|
-
handleFunctionalPlugin(plugin) {
|
|
1441
|
-
}
|
|
1442
|
-
handleViewPlugin(plugin) {
|
|
1443
|
-
}
|
|
1444
|
-
handleThemePlugin(plugin) {
|
|
1445
|
-
}
|
|
1446
|
-
handleSystemPlugin(plugin) {
|
|
1447
|
-
}
|
|
1448
|
-
};
|
|
1449
|
-
var pluginManager = new PluginManager(new LocalStorageAdapter());
|
|
1450
|
-
|
|
1451
|
-
// src/components/PluginSlot.tsx
|
|
1452
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
1453
|
-
var PluginSlot = ({
|
|
1454
|
-
slot,
|
|
1455
|
-
props = {},
|
|
1456
|
-
className,
|
|
1457
|
-
style,
|
|
1458
|
-
renderItem,
|
|
1459
|
-
skeleton,
|
|
1460
|
-
fallback
|
|
1461
|
-
}) => {
|
|
1462
|
-
const extensions = pluginManager.getExtensions(slot);
|
|
1463
|
-
const systemConfig = pluginManager.getSystemConfig("title") ? {
|
|
1464
|
-
title: pluginManager.getSystemConfig("title"),
|
|
1465
|
-
logo: pluginManager.getSystemConfig("logo"),
|
|
1466
|
-
version: pluginManager.getSystemConfig("version")
|
|
1467
|
-
} : void 0;
|
|
1468
|
-
const mergedProps = (0, import_react3.useMemo)(() => ({
|
|
1469
|
-
...props,
|
|
1470
|
-
systemConfig
|
|
1471
|
-
}), [props, systemConfig]);
|
|
1472
|
-
const items = (0, import_react3.useMemo)(() => {
|
|
1473
|
-
return extensions.map((ext, index) => {
|
|
1474
|
-
const Component2 = ext.component;
|
|
1475
|
-
const key = ext.meta?.key || `${ext.slot}-${ext.order || 0}-${index}`;
|
|
1476
|
-
return {
|
|
1477
|
-
key,
|
|
1478
|
-
extension: ext,
|
|
1479
|
-
component: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(PluginErrorBoundary, { pluginId: `extension-${key}`, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Component2, { ...mergedProps }) }, key)
|
|
1480
|
-
};
|
|
1481
|
-
});
|
|
1482
|
-
}, [extensions, mergedProps]);
|
|
1483
|
-
if (items.length === 0) {
|
|
1484
|
-
if (fallback) {
|
|
1485
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
1486
|
-
}
|
|
1487
|
-
if (skeleton) {
|
|
1488
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: `plugin-slot plugin-slot-${slot} plugin-slot-skeleton ${className || ""}`, style, children: skeleton });
|
|
1489
|
-
}
|
|
1490
|
-
return null;
|
|
1491
|
-
}
|
|
1492
|
-
if (items.length === 1 && slot === "root-layout" && !className && !style && !renderItem) {
|
|
1493
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: items[0].component });
|
|
1494
|
-
}
|
|
1495
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className: `plugin-slot plugin-slot-${slot} ${className || ""}`, style, children: renderItem ? items.map((item, index) => renderItem(item, index)) : items.map((item) => item.component) });
|
|
1496
|
-
};
|
|
1497
|
-
|
|
1498
|
-
// src/components/SlotSkeletons.tsx
|
|
1499
|
-
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1500
|
-
var SidebarIconSkeleton = ({ expanded = false }) => /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: `flex items-center transition-all duration-300 relative
|
|
1501
|
-
${expanded ? "w-full" : "w-12 justify-center"} px-3 h-11 rounded-xl`, children: [
|
|
1502
|
-
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "w-6 h-6 bg-slate-200 dark:bg-white/10 rounded-lg shrink-0 animate-pulse" }),
|
|
1503
|
-
expanded && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "ml-3 flex-1 h-4 bg-slate-200 dark:bg-white/10 rounded animate-pulse" })
|
|
1504
|
-
] });
|
|
1505
|
-
var StatusBarItemSkeleton = () => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "h-4 w-16 bg-slate-200 dark:bg-white/10 rounded animate-pulse" });
|
|
1506
|
-
var AvatarSkeleton = () => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: "w-10 h-10 rounded-full bg-slate-200 dark:bg-white/10 animate-pulse" });
|
|
1507
|
-
var BlockSkeleton = ({ className }) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: `bg-slate-200 dark:bg-white/10 rounded animate-pulse ${className || "w-full h-full"}` });
|
|
1508
|
-
|
|
1509
|
-
// src/domain/auto-loader.ts
|
|
1510
|
-
var import_meta = {};
|
|
1511
|
-
var logger8 = createLogger("AutoLoader");
|
|
1512
|
-
var resolvePluginRegistry = (options) => {
|
|
1513
|
-
const { modules, baseUrl, rules } = options;
|
|
1514
|
-
const registry = {};
|
|
1515
|
-
const compiledRules = rules.map((rule) => {
|
|
1516
|
-
const escapedSegment = rule.pathSegment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1517
|
-
return {
|
|
1518
|
-
...rule,
|
|
1519
|
-
regex: new RegExp(`${escapedSegment}/([^/]+)/src/index`)
|
|
1520
|
-
};
|
|
1521
|
-
});
|
|
1522
|
-
for (const relativePath in modules) {
|
|
1523
|
-
try {
|
|
1524
|
-
const path = new URL(relativePath, baseUrl).pathname;
|
|
1525
|
-
for (const rule of compiledRules) {
|
|
1526
|
-
const match = path.match(rule.regex);
|
|
1527
|
-
if (match && match[1]) {
|
|
1528
|
-
const dirName = match[1];
|
|
1529
|
-
const pluginId = `${rule.idPrefix}-${dirName}`;
|
|
1530
|
-
registry[pluginId] = modules[relativePath];
|
|
1531
|
-
logger8.info(`Found plugin: ${pluginId} at ${path}`);
|
|
1532
|
-
break;
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1535
|
-
} catch (e) {
|
|
1536
|
-
logger8.error(`Failed to resolve plugin at ${relativePath}:`, e);
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
return registry;
|
|
1540
|
-
};
|
|
1541
|
-
var discoverPlugins = (rules, baseUrl) => {
|
|
1542
|
-
const modules = import_meta.glob([
|
|
1543
|
-
"../../../../packages/plugins/*/src/index.{ts,tsx}",
|
|
1544
|
-
"../../../../apps/*/src/index.{ts,tsx}"
|
|
1545
|
-
]);
|
|
1546
|
-
return resolvePluginRegistry({
|
|
1547
|
-
modules,
|
|
1548
|
-
baseUrl,
|
|
1549
|
-
rules
|
|
1550
|
-
});
|
|
1551
|
-
};
|
|
1552
|
-
|
|
1553
|
-
// src/domain/models.ts
|
|
1554
|
-
var SUCCESS_CODE = "000000";
|
|
1555
|
-
|
|
1556
|
-
// src/plugin-context.tsx
|
|
1557
|
-
var import_react4 = require("react");
|
|
1558
|
-
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
1559
|
-
var PluginContext = (0, import_react4.createContext)(null);
|
|
1560
|
-
var PluginProvider = ({ manager, children }) => {
|
|
1561
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PluginContext.Provider, { value: manager, children });
|
|
1562
|
-
};
|
|
1563
|
-
var usePluginManager = () => {
|
|
1564
|
-
const context = (0, import_react4.useContext)(PluginContext);
|
|
1565
|
-
if (!context) {
|
|
1566
|
-
throw new Error("usePluginManager must be used within a PluginProvider");
|
|
1567
|
-
}
|
|
1568
|
-
return context;
|
|
1569
|
-
};
|
|
1570
|
-
|
|
1571
|
-
// src/api/adapters/axios-adapter.ts
|
|
1572
|
-
var import_axios = __toESM(require("axios"));
|
|
1573
|
-
var AxiosAdapter = class {
|
|
1574
|
-
client;
|
|
1575
|
-
constructor(baseURL = "/api", timeout = 1e4) {
|
|
1576
|
-
this.client = import_axios.default.create({
|
|
1577
|
-
baseURL,
|
|
1578
|
-
timeout
|
|
1579
|
-
});
|
|
1580
|
-
this.client.interceptors.response.use(
|
|
1581
|
-
(response) => response,
|
|
1582
|
-
(error) => Promise.reject(error)
|
|
1583
|
-
);
|
|
1584
|
-
}
|
|
1585
|
-
async request(config) {
|
|
1586
|
-
return this.client.request(config);
|
|
1587
|
-
}
|
|
1588
|
-
async stream(config, callbacks, endpointConfig) {
|
|
1589
|
-
const { onMessage, onError, onFinish } = callbacks;
|
|
1590
|
-
try {
|
|
1591
|
-
const baseURL = this.client.defaults.baseURL || "";
|
|
1592
|
-
let url = config.url;
|
|
1593
|
-
if (url && !url.startsWith("http") && baseURL) {
|
|
1594
|
-
const cleanBase = baseURL.endsWith("/") ? baseURL.slice(0, -1) : baseURL;
|
|
1595
|
-
const cleanUrl = url.startsWith("/") ? url : `/${url}`;
|
|
1596
|
-
url = `${cleanBase}${cleanUrl}`;
|
|
1597
|
-
}
|
|
1598
|
-
if (config.params) {
|
|
1599
|
-
const params = new URLSearchParams();
|
|
1600
|
-
Object.entries(config.params).forEach(([key, value]) => {
|
|
1601
|
-
if (value !== void 0) params.append(key, String(value));
|
|
1602
|
-
});
|
|
1603
|
-
url += (url.includes("?") ? "&" : "?") + params.toString();
|
|
1604
|
-
}
|
|
1605
|
-
const headers = {
|
|
1606
|
-
...this.client.defaults.headers.common || {},
|
|
1607
|
-
"Content-Type": "application/json",
|
|
1608
|
-
...config.headers || {}
|
|
1609
|
-
};
|
|
1610
|
-
const defaultHeaders = this.client.defaults.headers;
|
|
1611
|
-
if (defaultHeaders.Authorization) {
|
|
1612
|
-
headers["Authorization"] = defaultHeaders.Authorization;
|
|
1613
|
-
}
|
|
1614
|
-
if (defaultHeaders["x-token"]) {
|
|
1615
|
-
headers["x-token"] = defaultHeaders["x-token"];
|
|
1616
|
-
}
|
|
1617
|
-
const { signal, ...otherConfig } = config;
|
|
1618
|
-
const response = await fetch(url, {
|
|
1619
|
-
method: config.method,
|
|
1620
|
-
headers,
|
|
1621
|
-
body: config.data ? JSON.stringify(config.data) : void 0,
|
|
1622
|
-
signal
|
|
1623
|
-
});
|
|
1624
|
-
if (!response.ok) {
|
|
1625
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
1626
|
-
}
|
|
1627
|
-
if (!response.body) {
|
|
1628
|
-
throw new Error("\u54CD\u5E94\u4F53\u4E3A\u7A7A");
|
|
1629
|
-
}
|
|
1630
|
-
const reader = response.body.getReader();
|
|
1631
|
-
const decoder = new TextDecoder();
|
|
1632
|
-
let buffer = "";
|
|
1633
|
-
while (true) {
|
|
1634
|
-
const { done, value } = await reader.read();
|
|
1635
|
-
if (done) break;
|
|
1636
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1637
|
-
const parts = buffer.split("\n\n");
|
|
1638
|
-
buffer = parts.pop() || "";
|
|
1639
|
-
for (const part of parts) {
|
|
1640
|
-
if (onMessage) {
|
|
1641
|
-
onMessage(part + "\n\n");
|
|
1642
|
-
}
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
|
-
if (buffer && onMessage) {
|
|
1646
|
-
onMessage(buffer);
|
|
1647
|
-
}
|
|
1648
|
-
if (onFinish) onFinish();
|
|
1649
|
-
} catch (error) {
|
|
1650
|
-
if (onError) onError(error);
|
|
1651
|
-
}
|
|
1652
|
-
}
|
|
1653
|
-
};
|
|
1654
|
-
|
|
1655
|
-
// src/api/engine.ts
|
|
1656
|
-
var logger9 = createLogger("ApiEngine");
|
|
1657
|
-
var ApiEngine = class {
|
|
1658
|
-
adapter;
|
|
1659
|
-
config = {};
|
|
1660
|
-
interceptors = [];
|
|
1661
|
-
constructor(adapter) {
|
|
1662
|
-
this.adapter = adapter || new AxiosAdapter();
|
|
1663
|
-
}
|
|
1664
|
-
/**
|
|
1665
|
-
* 注册拦截器
|
|
1666
|
-
*/
|
|
1667
|
-
registerInterceptor(interceptor) {
|
|
1668
|
-
this.interceptors.push(interceptor);
|
|
1669
|
-
}
|
|
1670
|
-
/**
|
|
1671
|
-
* 移除拦截器
|
|
1672
|
-
*/
|
|
1673
|
-
unregisterInterceptor(interceptor) {
|
|
1674
|
-
this.interceptors = this.interceptors.filter((i) => i !== interceptor);
|
|
1675
|
-
}
|
|
1676
|
-
/**
|
|
1677
|
-
* 切换请求适配器
|
|
1678
|
-
* @param adapter 新的适配器实例
|
|
1679
|
-
*/
|
|
1680
|
-
useAdapter(adapter) {
|
|
1681
|
-
this.adapter = adapter;
|
|
1682
|
-
}
|
|
1683
|
-
/**
|
|
1684
|
-
* 注册 API 配置
|
|
1685
|
-
* @param config 配置对象
|
|
1686
|
-
*/
|
|
1687
|
-
register(config) {
|
|
1688
|
-
logger9.info("\u6B63\u5728\u6CE8\u518C API \u914D\u7F6E:", Object.keys(config));
|
|
1689
|
-
this.config = { ...this.config, ...config };
|
|
1690
|
-
}
|
|
1691
|
-
/**
|
|
1692
|
-
* 获取接口配置
|
|
1693
|
-
*/
|
|
1694
|
-
getEndpoint(module2, action) {
|
|
1695
|
-
return this.config[module2]?.[action];
|
|
1696
|
-
}
|
|
1697
|
-
/**
|
|
1698
|
-
* 发起 API 请求
|
|
1699
|
-
* @param module 模块名
|
|
1700
|
-
* @param action 动作名
|
|
1701
|
-
* @param data 请求数据 (Body 或 Query)
|
|
1702
|
-
* @param options 请求选项
|
|
1703
|
-
*/
|
|
1704
|
-
async call(module2, action, data, options = {}) {
|
|
1705
|
-
const endpoint = this.getEndpoint(module2, action);
|
|
1706
|
-
if (!endpoint) {
|
|
1707
|
-
logger9.warn(`\u672A\u627E\u5230 API \u5B9A\u4E49: ${module2}.${action} (\u5F53\u524D\u5DF2\u6CE8\u518C\u6A21\u5757: ${Object.keys(this.config).join(", ")})`);
|
|
1708
|
-
return Promise.resolve(void 0);
|
|
1709
|
-
}
|
|
1710
|
-
let requestConfig = await this.prepareRequestConfig(endpoint, data, options);
|
|
1711
|
-
let response;
|
|
1712
|
-
try {
|
|
1713
|
-
response = await this.adapter.request(requestConfig, endpoint);
|
|
1714
|
-
} catch (error) {
|
|
1715
|
-
if (error.response) {
|
|
1716
|
-
response = error.response;
|
|
1717
|
-
} else {
|
|
1718
|
-
throw error;
|
|
1719
|
-
}
|
|
1720
|
-
}
|
|
1721
|
-
if (this.interceptors.length > 0) {
|
|
1722
|
-
const context = this.createInterceptorContext(response, requestConfig);
|
|
1723
|
-
for (const interceptor of this.interceptors) {
|
|
1724
|
-
if (interceptor.interceptResponse) {
|
|
1725
|
-
const hijacked = await interceptor.interceptResponse(context);
|
|
1726
|
-
if (hijacked) {
|
|
1727
|
-
logger9.info("\u8BF7\u6C42\u88AB\u62E6\u622A\u5668\u52AB\u6301:", module2, action);
|
|
1728
|
-
return void 0;
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
}
|
|
1732
|
-
}
|
|
1733
|
-
if (response && response.status && (response.status < 200 || response.status >= 300)) {
|
|
1734
|
-
const responseData2 = this.isAxiosResponse(response) ? response.data : response;
|
|
1735
|
-
const isBaseResponse = this.isBaseResponse(responseData2);
|
|
1736
|
-
if (!isBaseResponse) {
|
|
1737
|
-
throw new Error(`Request failed with status ${response.status}`);
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
const responseData = this.isAxiosResponse(response) ? response.data : response;
|
|
1741
|
-
if (this.isBaseResponse(responseData)) {
|
|
1742
|
-
const res = responseData;
|
|
1743
|
-
const code = String(res.code);
|
|
1744
|
-
const isSuccess = code === SUCCESS_CODE || code === "200" || code === "0";
|
|
1745
|
-
if (!isSuccess) {
|
|
1746
|
-
const strategy = endpoint.errorStrategy || "reject";
|
|
1747
|
-
if (strategy === "reject") {
|
|
1748
|
-
logger9.error(`API \u8BF7\u6C42\u4E1A\u52A1\u9519\u8BEF (${module2}.${action}):`, res.message);
|
|
1749
|
-
throw new Error(res.message || `Request failed with code ${code}`);
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
}
|
|
1753
|
-
return responseData;
|
|
1754
|
-
}
|
|
1755
|
-
/**
|
|
1756
|
-
* 发起流式请求
|
|
1757
|
-
* @param module 模块名
|
|
1758
|
-
* @param action 动作名
|
|
1759
|
-
* @param data 请求数据
|
|
1760
|
-
* @param options 请求选项
|
|
1761
|
-
*/
|
|
1762
|
-
async stream(module2, action, data, options = {}) {
|
|
1763
|
-
const endpoint = this.getEndpoint(module2, action);
|
|
1764
|
-
if (!endpoint) {
|
|
1765
|
-
logger9.warn(`\u672A\u627E\u5230 API \u5B9A\u4E49: ${module2}.${action}\uFF0C\u8DF3\u8FC7\u6D41\u5F0F\u8BF7\u6C42\u3002`);
|
|
1766
|
-
return;
|
|
1767
|
-
}
|
|
1768
|
-
if (!this.adapter.stream) {
|
|
1769
|
-
logger9.warn("\u5F53\u524D API \u9002\u914D\u5668\u4E0D\u652F\u6301\u6D41\u5F0F\u4F20\u8F93\u3002");
|
|
1770
|
-
return;
|
|
1771
|
-
}
|
|
1772
|
-
const requestConfig = await this.prepareRequestConfig(endpoint, data, options);
|
|
1773
|
-
const callbacks = {
|
|
1774
|
-
onMessage: options.onMessage,
|
|
1775
|
-
onError: options.onError,
|
|
1776
|
-
onFinish: options.onFinish
|
|
1777
|
-
};
|
|
1778
|
-
await this.adapter.stream(requestConfig, callbacks, endpoint);
|
|
1779
|
-
}
|
|
1780
|
-
/**
|
|
1781
|
-
* 准备请求配置,应用 URL 参数替换和拦截器
|
|
1782
|
-
*/
|
|
1783
|
-
async prepareRequestConfig(endpoint, data, options) {
|
|
1784
|
-
let url = endpoint.url;
|
|
1785
|
-
const pathParams = options.params || {};
|
|
1786
|
-
url = url.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
|
|
1787
|
-
if (pathParams[key] !== void 0) return String(pathParams[key]);
|
|
1788
|
-
if (data && typeof data === "object" && data[key] !== void 0) return String(data[key]);
|
|
1789
|
-
return `:${key}`;
|
|
1790
|
-
});
|
|
1791
|
-
const method = endpoint.method;
|
|
1792
|
-
let requestConfig = {
|
|
1793
|
-
...options,
|
|
1794
|
-
url,
|
|
1795
|
-
method
|
|
1796
|
-
};
|
|
1797
|
-
if (method === "GET" || method === "DELETE") {
|
|
1798
|
-
requestConfig.params = data;
|
|
1799
|
-
} else {
|
|
1800
|
-
requestConfig.data = data;
|
|
1801
|
-
}
|
|
1802
|
-
if (this.interceptors.length > 0) {
|
|
1803
|
-
for (const interceptor of this.interceptors) {
|
|
1804
|
-
if (interceptor.interceptRequest) {
|
|
1805
|
-
requestConfig = await interceptor.interceptRequest(requestConfig);
|
|
1806
|
-
}
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1809
|
-
return requestConfig;
|
|
1810
|
-
}
|
|
1811
|
-
/**
|
|
1812
|
-
* 判断是否为 BaseResponse
|
|
1813
|
-
*/
|
|
1814
|
-
isBaseResponse(data) {
|
|
1815
|
-
return data && typeof data === "object" && "code" in data && ("message" in data || "data" in data);
|
|
1816
|
-
}
|
|
1817
|
-
/**
|
|
1818
|
-
* 严格判断是否为 AxiosResponse
|
|
1819
|
-
*/
|
|
1820
|
-
isAxiosResponse(res) {
|
|
1821
|
-
return res && typeof res === "object" && "data" in res && "status" in res && "headers" in res;
|
|
1822
|
-
}
|
|
1823
|
-
/**
|
|
1824
|
-
* 创建拦截上下文
|
|
1825
|
-
*/
|
|
1826
|
-
createInterceptorContext(response, config) {
|
|
1827
|
-
if (response && typeof response === "object" && "status" in response && "headers" in response) {
|
|
1828
|
-
return {
|
|
1829
|
-
response,
|
|
1830
|
-
status: response.status,
|
|
1831
|
-
headers: response.headers,
|
|
1832
|
-
data: response.data,
|
|
1833
|
-
config
|
|
1834
|
-
};
|
|
1835
|
-
}
|
|
1836
|
-
return {
|
|
1837
|
-
response,
|
|
1838
|
-
status: 200,
|
|
1839
|
-
headers: {},
|
|
1840
|
-
data: response,
|
|
1841
|
-
config
|
|
1842
|
-
};
|
|
1843
|
-
}
|
|
1844
|
-
};
|
|
1845
|
-
var apiEngine = new ApiEngine();
|
|
1846
|
-
|
|
1847
|
-
// src/utils/url.ts
|
|
1848
|
-
function normalizeParams(strategy = "last") {
|
|
1849
|
-
if (typeof window === "undefined") return new URLSearchParams();
|
|
1850
|
-
const { search, hash } = window.location;
|
|
1851
|
-
const searchParams = new URLSearchParams(search);
|
|
1852
|
-
const hashParams = new URLSearchParams(hash.split("?")[1] || "");
|
|
1853
|
-
const entries = [];
|
|
1854
|
-
searchParams.forEach((v, k) => entries.push([k, v, "search"]));
|
|
1855
|
-
hashParams.forEach((v, k) => entries.push([k, v, "hash"]));
|
|
1856
|
-
const keeper = /* @__PURE__ */ new Map();
|
|
1857
|
-
if (strategy === "first") {
|
|
1858
|
-
entries.forEach(([k, v]) => {
|
|
1859
|
-
if (!keeper.has(k)) keeper.set(k, v);
|
|
1860
|
-
});
|
|
1861
|
-
} else if (strategy === "last") {
|
|
1862
|
-
entries.forEach(([k, v]) => keeper.set(k, v));
|
|
1863
|
-
} else if (strategy === "search") {
|
|
1864
|
-
entries.forEach(([k, v, src]) => {
|
|
1865
|
-
if (src === "search") keeper.set(k, v);
|
|
1866
|
-
});
|
|
1867
|
-
hashParams.forEach((v, k) => {
|
|
1868
|
-
if (!keeper.has(k)) keeper.set(k, v);
|
|
1869
|
-
});
|
|
1870
|
-
} else if (strategy === "hash") {
|
|
1871
|
-
entries.forEach(([k, v, src]) => {
|
|
1872
|
-
if (src === "hash") keeper.set(k, v);
|
|
1873
|
-
});
|
|
1874
|
-
searchParams.forEach((v, k) => {
|
|
1875
|
-
if (!keeper.has(k)) keeper.set(k, v);
|
|
1876
|
-
});
|
|
1877
|
-
}
|
|
1878
|
-
return new URLSearchParams(Array.from(keeper.entries()));
|
|
1879
|
-
}
|
|
1880
|
-
function cleanUrlParams(keysToRemove) {
|
|
1881
|
-
if (typeof window === "undefined") return "";
|
|
1882
|
-
const { pathname, search, hash } = window.location;
|
|
1883
|
-
const searchParams = new URLSearchParams(search);
|
|
1884
|
-
keysToRemove.forEach((key) => searchParams.delete(key));
|
|
1885
|
-
const newSearch = searchParams.toString();
|
|
1886
|
-
const hashParts = hash.split("?");
|
|
1887
|
-
const hashPath = hashParts[0];
|
|
1888
|
-
const hashQuery = hashParts[1] || "";
|
|
1889
|
-
const hashParams = new URLSearchParams(hashQuery);
|
|
1890
|
-
keysToRemove.forEach((key) => hashParams.delete(key));
|
|
1891
|
-
const newHashQuery = hashParams.toString();
|
|
1892
|
-
const newHash = hashPath + (newHashQuery ? "?" + newHashQuery : "");
|
|
1893
|
-
return pathname + (newSearch ? "?" + newSearch : "") + newHash;
|
|
1894
|
-
}
|
|
1895
|
-
|
|
1896
|
-
// src/api/utils.ts
|
|
1897
|
-
var mergeMockData = (def, mock) => {
|
|
1898
|
-
const merged = JSON.parse(JSON.stringify(def));
|
|
1899
|
-
Object.keys(mock).forEach((key) => {
|
|
1900
|
-
if (merged[key]) {
|
|
1901
|
-
merged[key] = {
|
|
1902
|
-
...merged[key],
|
|
1903
|
-
...mock[key]
|
|
1904
|
-
};
|
|
1905
|
-
}
|
|
1906
|
-
});
|
|
1907
|
-
return merged;
|
|
1908
|
-
};
|
|
1909
|
-
function isMockMode() {
|
|
1910
|
-
const envMock = typeof process !== "undefined" && process.env.VITE_USE_MOCK === "true" || typeof window !== "undefined" && window.VITE_USE_MOCK === "true";
|
|
1911
|
-
if (typeof window !== "undefined") {
|
|
1912
|
-
const params = normalizeParams();
|
|
1913
|
-
const mockParam = params.get("mock");
|
|
1914
|
-
if (mockParam === "true") return true;
|
|
1915
|
-
if (mockParam === "false") return false;
|
|
1916
|
-
}
|
|
1917
|
-
return envMock;
|
|
1918
|
-
}
|
|
1919
|
-
function resolveApiModules(definitionsMap, mocksMap = {}) {
|
|
1920
|
-
const config = {};
|
|
1921
|
-
const getNamespace = (path) => {
|
|
1922
|
-
const fileName = path.split("/").pop() || "";
|
|
1923
|
-
return fileName.replace(/\.mock\.(ts|js|tsx|jsx|json)$/, "").replace(/\.(ts|js|tsx|jsx|json)$/, "");
|
|
1924
|
-
};
|
|
1925
|
-
Object.entries(definitionsMap).forEach(([path, module2]) => {
|
|
1926
|
-
if (path.includes(".mock.")) return;
|
|
1927
|
-
const namespace = getNamespace(path);
|
|
1928
|
-
if (!namespace || !module2.default) return;
|
|
1929
|
-
let apiDef = module2.default;
|
|
1930
|
-
const mockEntry = Object.entries(mocksMap).find(([mockPath]) => {
|
|
1931
|
-
return getNamespace(mockPath) === namespace && mockPath.includes(".mock.");
|
|
1932
|
-
});
|
|
1933
|
-
if (mockEntry) {
|
|
1934
|
-
const mockModule = mockEntry[1];
|
|
1935
|
-
const mockData = mockModule.default || mockModule;
|
|
1936
|
-
if (mockData) {
|
|
1937
|
-
apiDef = mergeMockData(apiDef, mockData);
|
|
1938
|
-
}
|
|
1939
|
-
}
|
|
1940
|
-
config[namespace] = apiDef;
|
|
1941
|
-
});
|
|
1942
|
-
return config;
|
|
1943
|
-
}
|
|
1944
|
-
|
|
1945
|
-
// src/utils/date.ts
|
|
1946
|
-
var import_dayjs = __toESM(require("dayjs"));
|
|
1947
|
-
var import_relativeTime = __toESM(require("dayjs/plugin/relativeTime"));
|
|
1948
|
-
var import_zh_cn = require("dayjs/locale/zh-cn");
|
|
1949
|
-
import_dayjs.default.extend(import_relativeTime.default);
|
|
1950
|
-
import_dayjs.default.locale("zh-cn");
|
|
1951
|
-
var dateUtils = {
|
|
1952
|
-
/**
|
|
1953
|
-
* 格式化日期为 YYYY-MM-DD
|
|
1954
|
-
*/
|
|
1955
|
-
formatDate(date) {
|
|
1956
|
-
return (0, import_dayjs.default)(date).format("YYYY-MM-DD");
|
|
1957
|
-
},
|
|
1958
|
-
/**
|
|
1959
|
-
* 格式化时间为 HH:mm:ss
|
|
1960
|
-
*/
|
|
1961
|
-
formatTime(date) {
|
|
1962
|
-
return (0, import_dayjs.default)(date).format("HH:mm:ss");
|
|
1963
|
-
},
|
|
1964
|
-
/**
|
|
1965
|
-
* 格式化日期时间为 YYYY-MM-DD HH:mm:ss
|
|
1966
|
-
*/
|
|
1967
|
-
formatDateTime(date) {
|
|
1968
|
-
return (0, import_dayjs.default)(date).format("YYYY-MM-DD HH:mm:ss");
|
|
1969
|
-
},
|
|
1970
|
-
/**
|
|
1971
|
-
* 获取当前时间戳(毫秒)
|
|
1972
|
-
*/
|
|
1973
|
-
now() {
|
|
1974
|
-
return (0, import_dayjs.default)().valueOf();
|
|
1975
|
-
},
|
|
1976
|
-
/**
|
|
1977
|
-
* 获取相对时间(例如:几分钟前)
|
|
1978
|
-
*/
|
|
1979
|
-
fromNow(date) {
|
|
1980
|
-
return (0, import_dayjs.default)(date).fromNow();
|
|
1981
|
-
},
|
|
1982
|
-
/**
|
|
1983
|
-
* 原始 dayjs 对象,用于更复杂的场景
|
|
1984
|
-
*/
|
|
1985
|
-
dayjs: import_dayjs.default
|
|
1986
|
-
};
|
|
1987
|
-
|
|
1988
|
-
// src/utils/index.ts
|
|
1989
|
-
var version = "1.0.0";
|
|
1990
|
-
|
|
1991
|
-
// src/hooks/use-storage-state.ts
|
|
1992
|
-
var import_react5 = require("react");
|
|
1993
|
-
function useStorageState(pluginId, key, options = {}) {
|
|
1994
|
-
const { defaultValue, scope = "plugin" } = options;
|
|
1995
|
-
const storageManager = pluginManager.getStorageManager();
|
|
1996
|
-
const getStorage = (0, import_react5.useCallback)(() => {
|
|
1997
|
-
const contextStorage = storageManager.getContextStorage(pluginId);
|
|
1998
|
-
return scope === "shared" ? contextStorage.shared : contextStorage;
|
|
1999
|
-
}, [pluginId, scope, storageManager]);
|
|
2000
|
-
const [state, setState] = (0, import_react5.useState)(() => {
|
|
2001
|
-
try {
|
|
2002
|
-
if (typeof window === "undefined") return defaultValue;
|
|
2003
|
-
const storage = getStorage();
|
|
2004
|
-
const val = storage.get(key);
|
|
2005
|
-
return val !== null ? val : defaultValue;
|
|
2006
|
-
} catch (e) {
|
|
2007
|
-
console.warn(`[useStorageState] Error reading key "${key}"`, e);
|
|
2008
|
-
return defaultValue;
|
|
2009
|
-
}
|
|
2010
|
-
});
|
|
2011
|
-
const setValue = (0, import_react5.useCallback)((value) => {
|
|
2012
|
-
try {
|
|
2013
|
-
const valueToStore = value instanceof Function ? value(state) : value;
|
|
2014
|
-
setState(valueToStore);
|
|
2015
|
-
const storage = getStorage();
|
|
2016
|
-
storage.set(key, valueToStore);
|
|
2017
|
-
} catch (error) {
|
|
2018
|
-
console.warn(`[useStorageState] Error setting key "${key}":`, error);
|
|
2019
|
-
}
|
|
2020
|
-
}, [key, state, getStorage]);
|
|
2021
|
-
return [state, setValue];
|
|
2022
|
-
}
|
|
2023
|
-
|
|
2024
|
-
// src/hooks/use-plugin-loader.ts
|
|
2025
|
-
var import_react6 = require("react");
|
|
2026
|
-
var logger10 = createLogger("PluginLoader");
|
|
2027
|
-
var usePluginLoader = (options) => {
|
|
2028
|
-
const [pluginsLoaded, setPluginsLoaded] = (0, import_react6.useState)(false);
|
|
2029
|
-
const [pluginVersion, setPluginVersion] = (0, import_react6.useState)(0);
|
|
2030
|
-
const loadingRef = (0, import_react6.useRef)(false);
|
|
2031
|
-
(0, import_react6.useEffect)(() => {
|
|
2032
|
-
const unsubscribe = pluginManager.subscribe(() => {
|
|
2033
|
-
logger10.debug("Plugin state changed, refreshing UI...");
|
|
2034
|
-
setPluginVersion((v) => v + 1);
|
|
2035
|
-
});
|
|
2036
|
-
const load = async () => {
|
|
2037
|
-
if (loadingRef.current || pluginsLoaded) return;
|
|
2038
|
-
loadingRef.current = true;
|
|
2039
|
-
try {
|
|
2040
|
-
const {
|
|
2041
|
-
discoveryRules = [],
|
|
2042
|
-
registry: manualRegistry = {},
|
|
2043
|
-
pluginConfigs,
|
|
2044
|
-
sharedContext = {},
|
|
2045
|
-
baseUrl = window.location.origin
|
|
2046
|
-
} = options;
|
|
2047
|
-
logger10.info("Starting to load plugins...");
|
|
2048
|
-
const discoveredRegistry = discoveryRules.length > 0 ? discoverPlugins(discoveryRules, baseUrl) : {};
|
|
2049
|
-
const finalRegistry = { ...discoveredRegistry, ...manualRegistry };
|
|
2050
|
-
if (options.systemConfig) {
|
|
2051
|
-
const { configManager: configManager2 } = await Promise.resolve().then(() => (init_config_manager(), config_manager_exports));
|
|
2052
|
-
configManager2.set("system", options.systemConfig);
|
|
2053
|
-
}
|
|
2054
|
-
await pluginManager.loadPlugins(pluginConfigs, finalRegistry);
|
|
2055
|
-
await pluginManager.initPlugins(sharedContext);
|
|
2056
|
-
setPluginsLoaded(true);
|
|
2057
|
-
logger10.info("Plugins loaded successfully");
|
|
2058
|
-
} catch (error) {
|
|
2059
|
-
logger10.error("Failed to load plugins:", error);
|
|
2060
|
-
} finally {
|
|
2061
|
-
loadingRef.current = false;
|
|
2062
|
-
}
|
|
2063
|
-
};
|
|
2064
|
-
load();
|
|
2065
|
-
return () => {
|
|
2066
|
-
unsubscribe();
|
|
2067
|
-
};
|
|
2068
|
-
}, []);
|
|
2069
|
-
return {
|
|
2070
|
-
pluginsLoaded,
|
|
2071
|
-
pluginVersion
|
|
2072
|
-
};
|
|
2073
|
-
};
|
|
2074
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2075
|
-
0 && (module.exports = {
|
|
2076
|
-
ApiEngine,
|
|
2077
|
-
ApiProvider,
|
|
2078
|
-
AvatarSkeleton,
|
|
2079
|
-
AxiosAdapter,
|
|
2080
|
-
BlockSkeleton,
|
|
2081
|
-
ConfigManager,
|
|
2082
|
-
DefaultEventBus,
|
|
2083
|
-
LocalStorageAdapter,
|
|
2084
|
-
LogLevel,
|
|
2085
|
-
Logger,
|
|
2086
|
-
PLUGIN_TYPES,
|
|
2087
|
-
PluginErrorBoundary,
|
|
2088
|
-
PluginManager,
|
|
2089
|
-
PluginProvider,
|
|
2090
|
-
PluginRuntime,
|
|
2091
|
-
PluginSandbox,
|
|
2092
|
-
PluginSlot,
|
|
2093
|
-
SUCCESS_CODE,
|
|
2094
|
-
ScopedStorageAdapter,
|
|
2095
|
-
ServiceRegistry,
|
|
2096
|
-
SidebarIconSkeleton,
|
|
2097
|
-
Slot,
|
|
2098
|
-
StatusBarItemSkeleton,
|
|
2099
|
-
StorageManager,
|
|
2100
|
-
apiEngine,
|
|
2101
|
-
cleanUrlParams,
|
|
2102
|
-
configManager,
|
|
2103
|
-
createLogger,
|
|
2104
|
-
dateUtils,
|
|
2105
|
-
discoverPlugins,
|
|
2106
|
-
isMockMode,
|
|
2107
|
-
logger,
|
|
2108
|
-
normalizeParams,
|
|
2109
|
-
pluginManager,
|
|
2110
|
-
resolveApiModules,
|
|
2111
|
-
resolvePluginRegistry,
|
|
2112
|
-
serviceRegistry,
|
|
2113
|
-
useApi,
|
|
2114
|
-
usePluginLoader,
|
|
2115
|
-
usePluginManager,
|
|
2116
|
-
useStorageState,
|
|
2117
|
-
version
|
|
2118
|
-
});
|