Spaces:
Paused
Paused
File size: 5,722 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | /**
* MCP PowerPoint Backend Service
* Integrates with MCP PowerPoint server for presentation generation
*/
import { eventBus } from '../../mcp/EventBus.js';
import { logger } from '../../utils/logger.js';
interface MCPClient {
callTool(toolName: string, params: Record<string, unknown>): Promise<unknown>;
}
interface SlideData {
title: string;
content: string[];
notes?: string;
imageUrl?: string;
}
interface PresentationConfig {
name: string;
title: string;
theme?: string;
author?: string;
}
/**
* MCPPowerPointBackend - Handles PowerPoint generation via MCP
*/
export class MCPPowerPointBackend {
private mcpClient: MCPClient | null = null;
private isInitialized = false;
private presentations: Map<string, { name: string; slides: SlideData[] }> = new Map();
constructor() {
this.setupEventListeners();
}
private setupEventListeners(): void {
eventBus.on('docgen:powerpoint:create', async (data) => {
try {
await this.createPresentation({
name: data.presentationId,
title: data.title,
theme: data.theme,
author: data.userId
});
eventBus.emit('docgen:powerpoint:created', {
presentationId: data.presentationId,
status: 'created'
});
} catch (error) {
logger.error('PowerPoint creation failed:', error);
eventBus.emit('docgen:powerpoint:error', {
presentationId: data.presentationId,
error: String(error)
});
}
});
}
/**
* Initialize MCP client connection
*/
async initialize(client?: MCPClient): Promise<void> {
if (client) {
this.mcpClient = client;
} else {
// Create mock client for development
this.mcpClient = this.createMockClient();
}
this.isInitialized = true;
logger.info('MCPPowerPointBackend initialized');
}
private createMockClient(): MCPClient {
return {
callTool: async (toolName: string, params: Record<string, unknown>) => {
logger.debug(`Mock MCP call: ${toolName}`, params);
// Simulate tool responses
switch (toolName) {
case 'create-presentation':
return { success: true, name: params.name };
case 'add-slide-title':
case 'add-slide-content':
case 'add-slide-bullet':
case 'add-slide-image':
return { success: true, slideIndex: Math.floor(Math.random() * 10) };
case 'generate-and-save-image':
return {
success: true,
image_path: `/images/${params.file_name || 'generated'}.png`
};
case 'save-presentation':
return {
success: true,
file_path: `/presentations/${params.presentation_name}.pptx`
};
default:
return { success: false, error: 'Unknown tool' };
}
}
};
}
/**
* Create a new presentation
*/
async createPresentation(config: PresentationConfig): Promise<string> {
if (!this.isInitialized || !this.mcpClient) {
throw new Error('MCPPowerPointBackend not initialized');
}
const result = await this.mcpClient.callTool('create-presentation', {
name: config.name,
title: config.title,
theme: config.theme || 'corporate'
}) as { success: boolean; name: string };
if (result.success) {
this.presentations.set(config.name, { name: config.name, slides: [] });
return config.name;
}
throw new Error('Failed to create presentation');
}
/**
* Add a slide to a presentation
*/
async addSlide(
presentationName: string,
slideType: 'title' | 'content' | 'bullet' | 'image' | 'two-column',
data: SlideData
): Promise<void> {
if (!this.mcpClient) {
throw new Error('MCPPowerPointBackend not initialized');
}
const toolName = `add-slide-${slideType}`;
await this.mcpClient.callTool(toolName, {
presentation_name: presentationName,
title: data.title,
content: data.content,
notes: data.notes,
image_url: data.imageUrl
});
// Track slide locally
const presentation = this.presentations.get(presentationName);
if (presentation) {
presentation.slides.push(data);
}
}
/**
* Generate an image using AI
*/
async generateImage(prompt: string, fileName: string): Promise<string> {
if (!this.mcpClient) {
throw new Error('MCPPowerPointBackend not initialized');
}
const result = await this.mcpClient.callTool('generate-and-save-image', {
prompt,
file_name: fileName
}) as { success: boolean; image_path: string };
return result.image_path;
}
/**
* Save the presentation
*/
async savePresentation(presentationName: string): Promise<string> {
if (!this.mcpClient) {
throw new Error('MCPPowerPointBackend not initialized');
}
const result = await this.mcpClient.callTool('save-presentation', {
presentation_name: presentationName
}) as { success: boolean; file_path: string };
return result.file_path;
}
/**
* Get presentation info
*/
getPresentation(name: string): { name: string; slides: SlideData[] } | undefined {
return this.presentations.get(name);
}
/**
* List all presentations
*/
listPresentations(): string[] {
return Array.from(this.presentations.keys());
}
}
// Singleton instance
let instance: MCPPowerPointBackend | null = null;
export function getMCPPowerPointBackend(): MCPPowerPointBackend {
if (!instance) {
instance = new MCPPowerPointBackend();
}
return instance;
}
export default MCPPowerPointBackend;
|