File size: 2,352 Bytes
ceb3821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { getPluginManager } from '../core/plugin-manager.js';
import { getRequestBody } from '../utils/common.js';
import { broadcastEvent } from './event-broadcast.js';

/**
 * 获取插件列表
 */
export async function handleGetPlugins(req, res) {
    try {
        const pluginManager = getPluginManager();
        const plugins = pluginManager.getPluginList();
        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({ plugins }));
        return true;
    } catch (error) {
        console.error('[UI API] Failed to get plugins:', error);
        res.writeHead(500, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
            error: {
                message: 'Failed to get plugins list: ' + error.message
            }
        }));
        return true;
    }
}

/**
 * 切换插件状态
 */
export async function handleTogglePlugin(req, res, pluginName) {
    try {
        const body = await getRequestBody(req);
        const { enabled } = body;

        if (typeof enabled !== 'boolean') {
            res.writeHead(400, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify({
                error: {
                    message: 'Enabled status must be a boolean'
                }
            }));
            return true;
        }

        const pluginManager = getPluginManager();
        await pluginManager.setPluginEnabled(pluginName, enabled);

        // 广播更新事件
        broadcastEvent('plugin_update', {
            action: 'toggle',
            pluginName,
            enabled,
            timestamp: new Date().toISOString()
        });

        res.writeHead(200, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
            success: true,
            message: `Plugin ${pluginName} ${enabled ? 'enabled' : 'disabled'} successfully`,
            plugin: {
                name: pluginName,
                enabled
            }
        }));
        return true;
    } catch (error) {
        console.error('[UI API] Failed to toggle plugin:', error);
        res.writeHead(500, { 'Content-Type': 'application/json' });
        res.end(JSON.stringify({
            error: {
                message: 'Failed to toggle plugin: ' + error.message
            }
        }));
        return true;
    }
}