@hivetechs/hive-ai
Version:
Real-time streaming AI consensus platform with HTTP+SSE MCP integration for Claude Code, VS Code, Cursor, and Windsurf - powered by OpenRouter's unified API
231 lines ⢠9.79 kB
JavaScript
/**
* Database Diagnostics and Repair Tool
* Helps troubleshoot profile saving issues across different environments
*/
import { getDatabase, initializeUnifiedDatabase, getAllPipelineProfiles } from '../../storage/unified-database.js';
/**
* Run comprehensive database diagnostics
*/
export async function runDatabaseDiagnostics() {
const result = {
databaseExists: false,
tablesExist: {
pipeline_profiles: false,
openrouter_models: false,
sync_metadata: false
},
modelCount: 0,
profileCount: 0,
schemaVersion: null,
issues: [],
canCreateProfiles: false
};
try {
console.log('š Running database diagnostics...\n');
// Check if database can be initialized
try {
await initializeUnifiedDatabase();
result.databaseExists = true;
console.log('ā
Database initialization successful');
}
catch (error) {
result.issues.push({
issue: 'database_init_failed',
severity: 'critical',
description: `Database initialization failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
fix: 'Try running: hive database reset'
});
return result;
}
const db = await getDatabase();
// Check table existence
const tables = await db.all(`
SELECT name FROM sqlite_master
WHERE type='table' AND name IN ('pipeline_profiles', 'openrouter_models', 'sync_metadata')
`);
const tableNames = tables.map(t => t.name);
result.tablesExist.pipeline_profiles = tableNames.includes('pipeline_profiles');
result.tablesExist.openrouter_models = tableNames.includes('openrouter_models');
result.tablesExist.sync_metadata = tableNames.includes('sync_metadata');
console.log('š Table Status:');
console.log(` pipeline_profiles: ${result.tablesExist.pipeline_profiles ? 'ā
' : 'ā'}`);
console.log(` openrouter_models: ${result.tablesExist.openrouter_models ? 'ā
' : 'ā'}`);
console.log(` sync_metadata: ${result.tablesExist.sync_metadata ? 'ā
' : 'ā'}`);
// Check for missing tables
if (!result.tablesExist.pipeline_profiles) {
result.issues.push({
issue: 'missing_profiles_table',
severity: 'critical',
description: 'pipeline_profiles table does not exist',
fix: 'Run database repair to recreate tables'
});
}
if (!result.tablesExist.openrouter_models) {
result.issues.push({
issue: 'missing_models_table',
severity: 'critical',
description: 'openrouter_models table does not exist',
fix: 'Run model sync to create and populate table'
});
}
// Count models
if (result.tablesExist.openrouter_models) {
try {
const modelCountResult = await db.get('SELECT COUNT(*) as count FROM openrouter_models WHERE is_active = 1');
result.modelCount = modelCountResult?.count || 0;
console.log(`š Active models: ${result.modelCount}`);
if (result.modelCount === 0) {
result.issues.push({
issue: 'no_models',
severity: 'critical',
description: 'No active models found in database',
fix: 'Run: hive models update'
});
}
else if (result.modelCount < 100) {
result.issues.push({
issue: 'few_models',
severity: 'warning',
description: `Only ${result.modelCount} models found (expected 300+)`,
fix: 'Run: hive models update'
});
}
}
catch (error) {
result.issues.push({
issue: 'model_count_failed',
severity: 'warning',
description: `Could not count models: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
}
// Count profiles
if (result.tablesExist.pipeline_profiles) {
try {
const profiles = await getAllPipelineProfiles();
result.profileCount = profiles.length;
console.log(`š„ Profiles: ${result.profileCount}`);
// Check for corrupted profiles
let corruptedCount = 0;
for (const profile of profiles) {
if (!profile.generator_model_internal_id || !profile.refiner_model_internal_id ||
!profile.validator_model_internal_id || !profile.curator_model_internal_id) {
corruptedCount++;
}
}
if (corruptedCount > 0) {
result.issues.push({
issue: 'corrupted_profiles',
severity: 'warning',
description: `${corruptedCount} profiles have missing internal model IDs`,
fix: 'Run profile repair to fix model references'
});
}
}
catch (error) {
result.issues.push({
issue: 'profile_count_failed',
severity: 'warning',
description: `Could not count profiles: ${error instanceof Error ? error.message : 'Unknown error'}`
});
}
}
// Check schema version
try {
const schemaResult = await db.get('PRAGMA user_version');
result.schemaVersion = schemaResult?.user_version?.toString() || null;
console.log(`šļø Schema version: ${result.schemaVersion || 'unknown'}`);
}
catch (error) {
result.issues.push({
issue: 'schema_version_failed',
severity: 'info',
description: 'Could not determine schema version'
});
}
// Check if profile creation should work
result.canCreateProfiles = result.tablesExist.pipeline_profiles &&
result.tablesExist.openrouter_models &&
result.modelCount > 0;
console.log(`šÆ Can create profiles: ${result.canCreateProfiles ? 'ā
' : 'ā'}`);
// Add final assessment
if (result.issues.length === 0) {
result.issues.push({
issue: 'all_good',
severity: 'info',
description: 'Database appears to be healthy',
});
}
}
catch (error) {
result.issues.push({
issue: 'diagnostic_failed',
severity: 'critical',
description: `Diagnostic failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
fix: 'Contact support with this error message'
});
}
return result;
}
/**
* Repair database issues automatically
*/
export async function repairDatabase() {
console.log('š§ Starting database repair...\n');
try {
// Force re-initialization
console.log('1. Reinitializing database...');
await initializeUnifiedDatabase();
console.log('ā
Database reinitialized');
// Sync models if needed
console.log('2. Checking model data...');
const { runUpdateModelsTool } = await import('./model-selection.js');
console.log('š Syncing models from OpenRouter...');
await runUpdateModelsTool();
console.log('ā
Model sync completed');
// Run diagnostics again
console.log('3. Verifying repair...');
const diagnostics = await runDatabaseDiagnostics();
if (diagnostics.canCreateProfiles) {
console.log('\nš Database repair successful! You can now create profiles.');
}
else {
console.log('\nā ļø Some issues remain:');
diagnostics.issues.filter(i => i.severity === 'critical').forEach(issue => {
console.log(` ā ${issue.description}`);
if (issue.fix)
console.log(` Fix: ${issue.fix}`);
});
}
}
catch (error) {
console.log(`ā Repair failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
throw error;
}
}
/**
* Reset database completely (nuclear option)
*/
export async function resetDatabase() {
console.log('ā¢ļø NUCLEAR OPTION: Resetting database completely...\n');
console.log('ā ļø This will delete ALL your profiles and configuration!');
try {
const db = await getDatabase();
// Drop all tables
console.log('šļø Dropping all tables...');
await db.exec('DROP TABLE IF EXISTS pipeline_profiles');
await db.exec('DROP TABLE IF EXISTS openrouter_models');
await db.exec('DROP TABLE IF EXISTS sync_metadata');
await db.exec('DROP TABLE IF EXISTS activity_log');
await db.exec('DROP TABLE IF EXISTS conversation_memory');
console.log('ā
All tables dropped');
// Reinitialize everything
await repairDatabase();
console.log('\nš Database reset complete! Start fresh with: hive quickstart');
}
catch (error) {
console.log(`ā Reset failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
throw error;
}
}
//# sourceMappingURL=database-diagnostics.js.map