UNPKG

@flexabrain/mcp-server

Version:

Advanced electrical schematic analysis MCP server with rail engineering expertise

281 lines 13.3 kB
// Simple UUID generator fallback function generateId() { return Math.random().toString(36).substring(2) + Date.now().toString(36); } export class SchematicDatabase { db; constructor(connection) { this.db = connection; } // Document operations async createDocument(doc) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO schematic_documents ( id, filename, original_path, document_type, title, version, subject, creator, creation_date, modification_date, pages_count, rail_system_type, voltage_system, upload_timestamp, processing_status, processing_options, metadata, created_by, updated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, doc.filename, doc.original_path, doc.document_type, doc.title, doc.version, doc.subject, doc.creator, doc.creation_date, doc.modification_date, doc.pages_count, doc.rail_system_type, doc.voltage_system, now, doc.processing_status, JSON.stringify(doc.processing_options), JSON.stringify(doc.metadata), doc.created_by, now ]); return id; } async getDocument(id) { const results = await this.db.query('SELECT * FROM schematic_documents WHERE id = ?', [id]); if (results.length === 0) return null; const row = results[0]; const result = { id: row.id, filename: row.filename, original_path: row.original_path, document_type: row.document_type, title: row.title, version: row.version, subject: row.subject, creator: row.creator, pages_count: row.pages_count, rail_system_type: row.rail_system_type, voltage_system: row.voltage_system, upload_timestamp: new Date(row.upload_timestamp), processing_status: row.processing_status, processing_options: row.processing_options, metadata: row.metadata, created_by: row.created_by, updated_at: new Date(row.updated_at) }; if (row.creation_date) { result.creation_date = new Date(row.creation_date); } if (row.modification_date) { result.modification_date = new Date(row.modification_date); } if (row.processing_started_at) { result.processing_started_at = new Date(row.processing_started_at); } if (row.processing_completed_at) { result.processing_completed_at = new Date(row.processing_completed_at); } return result; } async updateDocumentStatus(id, status, startedAt, completedAt) { await this.db.execute(` UPDATE schematic_documents SET processing_status = ?, processing_started_at = ?, processing_completed_at = ?, updated_at = ? WHERE id = ? `, [status, startedAt, completedAt, new Date(), id]); } async getDocumentsByStatus(status) { const results = await this.db.query('SELECT * FROM schematic_documents WHERE processing_status = ? ORDER BY upload_timestamp DESC', [status]); return results.map(row => { const result = { id: row.id, filename: row.filename, original_path: row.original_path, document_type: row.document_type, title: row.title, version: row.version, subject: row.subject, creator: row.creator, pages_count: row.pages_count, rail_system_type: row.rail_system_type, voltage_system: row.voltage_system, upload_timestamp: new Date(row.upload_timestamp), processing_status: row.processing_status, processing_options: row.processing_options, metadata: row.metadata, created_by: row.created_by, updated_at: new Date(row.updated_at) }; if (row.creation_date) { result.creation_date = new Date(row.creation_date); } if (row.modification_date) { result.modification_date = new Date(row.modification_date); } if (row.processing_started_at) { result.processing_started_at = new Date(row.processing_started_at); } if (row.processing_completed_at) { result.processing_completed_at = new Date(row.processing_completed_at); } return result; }); } // Page operations async createPage(page) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO schematic_pages ( id, document_id, page_number, page_title, schematic_type, image_path, image_dimensions, quality_score, processing_time, processing_results, technical_features, created_at, updated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, page.document_id, page.page_number, page.page_title, page.schematic_type, page.image_path, JSON.stringify(page.image_dimensions), page.quality_score, page.processing_time, JSON.stringify(page.processing_results), JSON.stringify(page.technical_features), now, now ]); return id; } async getPagesByDocument(documentId) { const results = await this.db.query('SELECT * FROM schematic_pages WHERE document_id = ? ORDER BY page_number', [documentId]); return results.map(row => ({ id: row.id, document_id: row.document_id, page_number: row.page_number, page_title: row.page_title, schematic_type: row.schematic_type, image_path: row.image_path, image_dimensions: row.image_dimensions, quality_score: row.quality_score, processing_time: row.processing_time, processing_results: row.processing_results, technical_features: row.technical_features, created_at: new Date(row.created_at), updated_at: new Date(row.updated_at) })); } // Component operations async createComponent(component) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO component_instances ( id, document_id, page_id, component_id, component_type, component_category, bounding_box, confidence_score, pattern_matched, specifications, safety_level, compliance_status, rail_system_context, created_at, updated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, component.document_id, component.page_id, component.component_id, component.component_type, component.component_category, JSON.stringify(component.bounding_box), component.confidence_score, component.pattern_matched, JSON.stringify(component.specifications), component.safety_level, component.compliance_status, JSON.stringify(component.rail_system_context), now, now ]); return id; } async getComponentsByDocument(documentId) { const results = await this.db.query('SELECT * FROM component_instances WHERE document_id = ? ORDER BY confidence_score DESC', [documentId]); return results.map(row => ({ id: row.id, document_id: row.document_id, page_id: row.page_id, component_id: row.component_id, component_type: row.component_type, component_category: row.component_category, bounding_box: row.bounding_box, confidence_score: row.confidence_score, pattern_matched: row.pattern_matched, specifications: row.specifications, safety_level: row.safety_level, compliance_status: row.compliance_status, rail_system_context: row.rail_system_context, created_at: new Date(row.created_at), updated_at: new Date(row.updated_at) })); } // Cross-page reference operations async createCrossPageReference(ref) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO cross_page_references ( id, document_id, source_component_id, target_component_id, source_page_number, target_page_number, reference_type, reference_text, confidence_score, verified, verification_method, verified_by, verified_at, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, ref.document_id, ref.source_component_id, ref.target_component_id, ref.source_page_number, ref.target_page_number, ref.reference_type, ref.reference_text, ref.confidence_score, ref.verified ? 1 : 0, ref.verification_method, ref.verified_by, ref.verified_at, now ]); return id; } // AI Agent analysis operations async createAgentAnalysis(analysis) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO agent_analysis_results ( id, document_id, component_id, agent_name, analysis_type, analysis_results, confidence_score, risk_assessment, recommendations, prediction_data, anomaly_data, business_insights, processing_time, created_at, expires_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, analysis.document_id, analysis.component_id, analysis.agent_name, analysis.analysis_type, JSON.stringify(analysis.analysis_results), analysis.confidence_score, JSON.stringify(analysis.risk_assessment), JSON.stringify(analysis.recommendations), JSON.stringify(analysis.prediction_data), JSON.stringify(analysis.anomaly_data), JSON.stringify(analysis.business_insights), analysis.processing_time, now, analysis.expires_at ]); return id; } // Logging operations async logProcessingEvent(log) { const id = generateId(); const now = new Date(); await this.db.execute(` INSERT INTO processing_logs ( id, document_id, page_id, log_level, error_code, message, component_id, severity, recovery_suggestions, stack_trace, created_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `, [ id, log.document_id, log.page_id, log.log_level, log.error_code, log.message, log.component_id, log.severity, JSON.stringify(log.recovery_suggestions), log.stack_trace, now ]); return id; } async getProcessingLogs(documentId, logLevel) { const query = logLevel ? 'SELECT * FROM processing_logs WHERE document_id = ? AND log_level = ? ORDER BY created_at DESC' : 'SELECT * FROM processing_logs WHERE document_id = ? ORDER BY created_at DESC'; const params = logLevel ? [documentId, logLevel] : [documentId]; const results = await this.db.query(query, params); return results.map(row => ({ id: row.id, document_id: row.document_id, page_id: row.page_id, log_level: row.log_level, error_code: row.error_code, message: row.message, component_id: row.component_id, severity: row.severity, recovery_suggestions: row.recovery_suggestions, stack_trace: row.stack_trace, created_at: new Date(row.created_at) })); } // Statistics operations async getDocumentStatistics(documentId) { const [componentStats] = await this.db.query(` SELECT COUNT(*) as total_components, COUNT(CASE WHEN confidence_score >= 0.8 THEN 1 END) as high_confidence_components FROM component_instances WHERE document_id = ? `, [documentId]); const [crossRefStats] = await this.db.query('SELECT COUNT(*) as cross_references FROM cross_page_references WHERE document_id = ?', [documentId]); const [qualityStats] = await this.db.query('SELECT AVG(quality_score) as avg_quality FROM schematic_pages WHERE document_id = ?', [documentId]); const [errorStats] = await this.db.query('SELECT COUNT(*) as processing_errors FROM processing_logs WHERE document_id = ? AND log_level = ?', [documentId, 'error']); const [analysisStats] = await this.db.query('SELECT COUNT(*) as agent_analyses FROM agent_analysis_results WHERE document_id = ?', [documentId]); return { totalComponents: componentStats?.total_components || 0, highConfidenceComponents: componentStats?.high_confidence_components || 0, crossReferences: crossRefStats[0]?.cross_references || 0, avgPageQuality: qualityStats[0]?.avg_quality || 0, processingErrors: errorStats[0]?.processing_errors || 0, agentAnalyses: analysisStats[0]?.agent_analyses || 0 }; } async close() { await this.db.close(); } } export default SchematicDatabase; //# sourceMappingURL=database-interface.js.map