Spaces:
Sleeping
Sleeping
File size: 4,162 Bytes
525d39f | 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 | import { Injectable, NotFoundException } from '@nestjs/common';
import { PluginLoaderService, PluginStatus } from '../../core/plugins';
import { PluginDto } from './dto/plugin.dto';
@Injectable()
export class PluginsService {
constructor(private readonly pluginLoader: PluginLoaderService) {}
findAll(): PluginDto[] {
const plugins = this.pluginLoader.getAllPlugins();
return plugins.map(plugin => ({
id: plugin.manifest.id,
name: plugin.manifest.name,
version: plugin.manifest.version,
type: plugin.manifest.type,
description: plugin.manifest.description,
author: plugin.manifest.author,
status: plugin.status,
config: plugin.config,
builtIn: plugin.manifest.id === 'whatsapp-web.js', // Built-in engines
provides: plugin.manifest.provides ?? [],
configSchema: plugin.manifest.configSchema,
loadedAt: plugin.loadedAt?.toISOString(),
enabledAt: plugin.enabledAt?.toISOString(),
error: plugin.error,
}));
}
findOne(id: string): PluginDto {
const plugin = this.pluginLoader.getPlugin(id);
if (!plugin) {
throw new NotFoundException(`Plugin ${id} not found`);
}
return {
id: plugin.manifest.id,
name: plugin.manifest.name,
version: plugin.manifest.version,
type: plugin.manifest.type,
description: plugin.manifest.description,
author: plugin.manifest.author,
status: plugin.status,
config: plugin.config,
builtIn: plugin.manifest.id === 'whatsapp-web.js',
provides: plugin.manifest.provides ?? [],
configSchema: plugin.manifest.configSchema,
loadedAt: plugin.loadedAt?.toISOString(),
enabledAt: plugin.enabledAt?.toISOString(),
error: plugin.error,
};
}
async enable(id: string): Promise<{ success: boolean; message: string }> {
const plugin = this.pluginLoader.getPlugin(id);
if (!plugin) {
throw new NotFoundException(`Plugin ${id} not found`);
}
if (plugin.status === PluginStatus.ENABLED) {
return { success: true, message: `Plugin ${id} is already enabled` };
}
try {
await this.pluginLoader.enablePlugin(id);
return { success: true, message: `Plugin ${id} enabled successfully` };
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : String(error),
};
}
}
async disable(id: string): Promise<{ success: boolean; message: string }> {
const plugin = this.pluginLoader.getPlugin(id);
if (!plugin) {
throw new NotFoundException(`Plugin ${id} not found`);
}
if (plugin.status !== PluginStatus.ENABLED) {
return { success: true, message: `Plugin ${id} is not enabled` };
}
try {
await this.pluginLoader.disablePlugin(id);
return { success: true, message: `Plugin ${id} disabled successfully` };
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : String(error),
};
}
}
updateConfig(id: string, config: Record<string, unknown>): { success: boolean; message: string } {
const plugin = this.pluginLoader.getPlugin(id);
if (!plugin) {
throw new NotFoundException(`Plugin ${id} not found`);
}
try {
this.pluginLoader.updatePluginConfig(id, config);
return { success: true, message: `Plugin ${id} configuration updated` };
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : String(error),
};
}
}
async healthCheck(id: string): Promise<{ healthy: boolean; message?: string }> {
const plugin = this.pluginLoader.getPlugin(id);
if (!plugin) {
throw new NotFoundException(`Plugin ${id} not found`);
}
if (!plugin.instance?.healthCheck) {
return { healthy: true, message: 'Plugin does not implement health check' };
}
try {
return await plugin.instance.healthCheck();
} catch (error) {
return {
healthy: false,
message: error instanceof Error ? error.message : String(error),
};
}
}
}
|