File size: 5,056 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * Plugin System
 * Extensible architecture for adding custom functionality
 */

export interface PluginMetadata {
    name: string;
    version: string;
    description: string;
    author: string;
    dependencies?: string[];
    permissions?: string[];
}

export interface PluginHooks {
    onLoad?: () => Promise<void>;
    onUnload?: () => Promise<void>;
    onMessage?: (message: any) => Promise<any>;
    onQuery?: (query: string) => Promise<any>;
    onResult?: (result: any) => Promise<any>;
}

export interface Plugin {
    metadata: PluginMetadata;
    hooks: PluginHooks;
    // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
    api?: Record<string, Function>;
}

export class PluginManager {
    private plugins: Map<string, Plugin> = new Map();
    private loadedPlugins: Set<string> = new Set();

    /**
     * Register a plugin
     */
    async registerPlugin(plugin: Plugin): Promise<void> {
        const { name, version } = plugin.metadata;

        // Check dependencies
        if (plugin.metadata.dependencies) {
            for (const dep of plugin.metadata.dependencies) {
                if (!this.loadedPlugins.has(dep)) {
                    throw new Error(`Plugin ${name} requires ${dep} which is not loaded`);
                }
            }
        }

        // Store plugin
        this.plugins.set(name, plugin);

        // Call onLoad hook
        if (plugin.hooks.onLoad) {
            await plugin.hooks.onLoad();
        }

        this.loadedPlugins.add(name);
        console.log(`✅ Plugin loaded: ${name} v${version}`);
    }

    /**
     * Unload a plugin
     */
    async unloadPlugin(name: string): Promise<void> {
        const plugin = this.plugins.get(name);
        if (!plugin) {
            throw new Error(`Plugin ${name} not found`);
        }

        // Check if other plugins depend on this
        for (const [pluginName, p] of this.plugins.entries()) {
            if (p.metadata.dependencies?.includes(name)) {
                throw new Error(`Cannot unload ${name}: ${pluginName} depends on it`);
            }
        }

        // Call onUnload hook
        if (plugin.hooks.onUnload) {
            await plugin.hooks.onUnload();
        }

        this.plugins.delete(name);
        this.loadedPlugins.delete(name);
        console.log(`🗑️  Plugin unloaded: ${name}`);
    }

    /**
     * Execute hook across all plugins
     */
    async executeHook(
        hookName: keyof PluginHooks,
        ...args: any[]
    ): Promise<any[]> {
        const results: any[] = [];

        for (const plugin of this.plugins.values()) {
            const hook = plugin.hooks[hookName];
            if (hook && typeof hook === 'function') {
                try {
                    const result = await (hook as (...args: any[]) => Promise<any>)(...args);
                    results.push(result);
                } catch (error) {
                    console.error(`Error in plugin ${plugin.metadata.name} hook ${hookName}:`, error);
                }
            }
        }

        return results;
    }

    /**
     * Call plugin API method
     */
    async callPluginAPI(
        pluginName: string,
        method: string,
        ...args: any[]
    ): Promise<any> {
        const plugin = this.plugins.get(pluginName);
        if (!plugin) {
            throw new Error(`Plugin ${pluginName} not found`);
        }

        if (!plugin.api || !plugin.api[method]) {
            throw new Error(`Plugin ${pluginName} does not have method ${method}`);
        }

        return plugin.api[method](...args);
    }

    /**
     * Get loaded plugins
     */
    getLoadedPlugins(): PluginMetadata[] {
        return Array.from(this.plugins.values()).map(p => p.metadata);
    }

    /**
     * Check if plugin is loaded
     */
    isPluginLoaded(name: string): boolean {
        return this.loadedPlugins.has(name);
    }

    /**
     * Get plugin by name
     */
    getPlugin(name: string): Plugin | undefined {
        return this.plugins.get(name);
    }
}

/**
 * Example plugin: Custom Search Enhancer
 */
export const exampleSearchPlugin: Plugin = {
    metadata: {
        name: 'custom-search-enhancer',
        version: '1.0.0',
        description: 'Enhances search results with custom logic',
        author: 'WidgeTDC Team',
        permissions: ['search', 'modify_results'],
    },
    hooks: {
        onLoad: async () => {
            console.log('Custom Search Enhancer loaded');
        },
        onQuery: async (query: string) => {
            // Enhance query
            return `enhanced: ${query}`;
        },
        onResult: async (result: any) => {
            // Modify results
            return {
                ...result,
                enhanced: true,
                timestamp: new Date().toISOString(),
            };
        },
    },
    api: {
        customMethod: (param: string) => {
            return `Custom method called with: ${param}`;
        },
    },
};

export const pluginManager = new PluginManager();