Add reactive plugin config UI with SSE support
Introduces a reactive plugin configuration system with dynamic schema updates via server-sent events (SSE). Adds new fields and controller interfaces to the plugin manager, updates the built-in plugin to demonstrate dynamic config fields, and implements backend and frontend logic for real-time config UI updates. Also updates napcat-types to 0.0.10.
This commit is contained in:
1 parent
9f62570fc2
commit
a4a93c520f
8 files changed
+590
-94
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import type { ActionMap } from 'napcat-types/napcat-onebot/action/index';
|
||||
import { EventType } from 'napcat-types/napcat-onebot/event/index';
|
||||
import type { PluginModule, PluginLogger, PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
||||
import type { PluginModule, PluginLogger, PluginConfigSchema, PluginConfigUIController } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
||||
import type { OB11Message, OB11PostSendMsg } from 'napcat-types/napcat-onebot/types/index';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { NetworkAdapterConfig } from 'napcat-types/napcat-onebot/config/config';
|
||||
|
||||
|
||||
let startTime: number = Date.now();
|
||||
let logger: PluginLogger | null = null;
|
||||
|
||||
@@ -14,6 +16,8 @@ interface BuiltinPluginConfig {
|
||||
description: string;
|
||||
theme?: string;
|
||||
features?: string[];
|
||||
apiUrl?: string;
|
||||
apiEndpoints?: string[];
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
@@ -30,9 +34,11 @@ const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
|
||||
logger = ctx.logger;
|
||||
logger.info('NapCat 内置插件已初始化');
|
||||
plugin_config_ui = ctx.NapCatConfig.combine(
|
||||
ctx.NapCatConfig.html('<div style="padding: 10px; background: rgba(0,0,0,0.05); border-radius: 8px;"><h3>👋 Welcome to NapCat Builtin Plugin</h3><p>This is a demonstration of the plugin configuration interface.</p></div>'),
|
||||
ctx.NapCatConfig.html('<div style="padding: 10px; background: rgba(0,0,0,0.05); border-radius: 8px;"><h3>👋 Welcome to NapCat Builtin Plugin</h3><p>This is a demonstration of the plugin configuration interface with reactive fields.</p></div>'),
|
||||
ctx.NapCatConfig.text('prefix', 'Command Prefix', '#napcat', 'The prefix to trigger the version info command'),
|
||||
ctx.NapCatConfig.boolean('enableReply', 'Enable Reply', true, 'Switch to enable or disable the reply functionality'),
|
||||
// 代表监听 apiUrl 字段的变化
|
||||
{ ...ctx.NapCatConfig.text('apiUrl', 'API URL', '', 'Enter an API URL to load available endpoints'), reactive: true },
|
||||
ctx.NapCatConfig.select('theme', 'Theme Selection', [
|
||||
{ label: 'Light Mode', value: 'light' },
|
||||
{ label: 'Dark Mode', value: 'dark' },
|
||||
@@ -59,11 +65,11 @@ const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
|
||||
|
||||
};
|
||||
|
||||
export const plugin_get_config = async () => {
|
||||
export const plugin_get_config: PluginModule['plugin_get_config'] = async () => {
|
||||
return currentConfig;
|
||||
};
|
||||
|
||||
export const plugin_set_config = async (ctx: any, config: BuiltinPluginConfig) => {
|
||||
export const plugin_set_config: PluginModule['plugin_set_config'] = async (ctx, config: BuiltinPluginConfig) => {
|
||||
currentConfig = config;
|
||||
if (ctx && ctx.configPath) {
|
||||
try {
|
||||
@@ -80,6 +86,74 @@ export const plugin_set_config = async (ctx: any, config: BuiltinPluginConfig) =
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 响应式配置控制器 - 当插件配置界面打开时调用
|
||||
* 用于初始化动态 UI 控制
|
||||
*/
|
||||
export const plugin_config_controller: PluginModule['plugin_config_controller'] = async (_ctx, ui, initialConfig) => {
|
||||
logger?.info('配置控制器已初始化', initialConfig);
|
||||
|
||||
// 如果初始配置中有 apiUrl,立即加载端点
|
||||
if (initialConfig['apiUrl']) {
|
||||
await loadEndpointsForUrl(ui, initialConfig['apiUrl'] as string);
|
||||
}
|
||||
|
||||
// 返回清理函数
|
||||
return () => {
|
||||
logger?.info('配置控制器已清理');
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 响应式字段变化处理 - 当标记为 reactive 的字段值变化时调用
|
||||
*/
|
||||
export const plugin_on_config_change: PluginModule['plugin_on_config_change'] = async (_ctx, ui, key, value, _currentConfig: Partial<BuiltinPluginConfig>) => {
|
||||
logger?.info(`配置字段变化: ${key} = ${value}`);
|
||||
|
||||
if (key === 'apiUrl') {
|
||||
await loadEndpointsForUrl(ui, value as string);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 根据 API URL 动态加载端点列表
|
||||
*/
|
||||
async function loadEndpointsForUrl (ui: PluginConfigUIController, apiUrl: string) {
|
||||
if (!apiUrl) {
|
||||
// URL 为空时,移除端点选择字段
|
||||
ui.removeField('apiEndpoints');
|
||||
return;
|
||||
}
|
||||
|
||||
// 模拟从 API 获取端点列表(实际使用时可以 fetch 真实 API)
|
||||
const mockEndpoints = [
|
||||
{ label: `${apiUrl}/users`, value: '/users' },
|
||||
{ label: `${apiUrl}/posts`, value: '/posts' },
|
||||
{ label: `${apiUrl}/comments`, value: '/comments' },
|
||||
{ label: `${apiUrl}/albums`, value: '/albums' },
|
||||
];
|
||||
|
||||
// 动态添加或更新端点选择字段
|
||||
const currentSchema = ui.getCurrentConfig();
|
||||
if ('apiEndpoints' in currentSchema) {
|
||||
// 更新现有字段的选项
|
||||
ui.updateField('apiEndpoints', {
|
||||
options: mockEndpoints,
|
||||
description: `从 ${apiUrl} 加载的端点`
|
||||
});
|
||||
} else {
|
||||
// 添加新字段
|
||||
ui.addField({
|
||||
key: 'apiEndpoints',
|
||||
type: 'multi-select',
|
||||
label: 'API Endpoints',
|
||||
description: `从 ${apiUrl} 加载的端点`,
|
||||
options: mockEndpoints,
|
||||
default: []
|
||||
}, 'apiUrl');
|
||||
}
|
||||
}
|
||||
|
||||
const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) => {
|
||||
if (currentConfig.enableReply === false) {
|
||||
return;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
"description": "NapCat 内置插件",
|
||||
"author": "NapNeko",
|
||||
"dependencies": {
|
||||
"napcat-types": "0.0.9"
|
||||
"napcat-types": "0.0.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.1"
|
||||
|
||||
Reference in new issue
Block a user