@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
343 lines • 15 kB
JavaScript
/**
* Knowledge Base Seeder
*
* Seeds the unified database with essential knowledge from the built-in
* knowledge base, ensuring new users have immediate access to comprehensive
* documentation and MCP tool information.
*/
import { getDatabase, getConfig, setConfig } from '../../storage/unified-database.js';
import { KnowledgeBaseHelper } from './knowledge-base-help.js';
import { v4 as uuidv4 } from 'uuid';
export class KnowledgeBaseSeeder {
db;
knowledgeBase;
constructor() {
this.knowledgeBase = new KnowledgeBaseHelper();
}
/**
* Check if knowledge base has been seeded for current version
*/
async isSeeded() {
try {
const db = await getDatabase();
// Check if we have current version seeded
const currentVersion = await this.getCurrentVersion();
const versionSeeded = await getConfig(`knowledge_base_seeded_v${currentVersion}`);
if (versionSeeded === 'true') {
return true;
}
// Fallback: check general seeding
const result = await db.get(`SELECT COUNT(*) as count FROM knowledge_conversations WHERE source_of_truth LIKE '%Knowledge Base Seed%'`);
return result.count > 0;
}
catch (error) {
// If database is read-only or not accessible, assume not seeded
if (error.code === 'SQLITE_READONLY' || error.message?.includes('read-only')) {
return false;
}
console.debug('Error checking seed status:', error.message);
return false;
}
}
/**
* Get current package version for cache invalidation
*/
async getCurrentVersion() {
try {
// Try to read from package.json
const fs = await import('fs');
const path = await import('path');
const { fileURLToPath } = await import('url');
// First try from the module's location
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packagePath = path.join(__dirname, '..', '..', '..', 'package.json');
if (fs.existsSync(packagePath)) {
const pkg = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
return pkg.version;
}
// Fallback to process.cwd()
const cwdPackagePath = path.join(process.cwd(), 'package.json');
if (fs.existsSync(cwdPackagePath)) {
const pkg = JSON.parse(fs.readFileSync(cwdPackagePath, 'utf8'));
return pkg.version;
}
return 'unknown';
}
catch (error) {
console.warn('Failed to read package version:', error);
return 'unknown';
}
}
/**
* Seed the database with essential knowledge
*/
async seedKnowledgeBase() {
try {
// Ensure database is properly initialized in user's home directory
const db = await getDatabase();
// Skip if already seeded
if (await this.isSeeded()) {
console.log('✅ Knowledge base already seeded');
return;
}
console.log('🌱 Seeding knowledge base...');
// Test if we can write to the database first with a simple write operation
try {
await db.run('BEGIN TRANSACTION');
await db.run('ROLLBACK');
}
catch (testError) {
if (testError.code === 'SQLITE_READONLY') {
console.debug('Database is read-only, skipping knowledge base seeding');
return;
}
throw testError;
}
// Essential knowledge topics to seed
const knowledgeTopics = [
{
question: "What MCP tools are available in Hive AI?",
category: "mcp_tools",
keywords: ["mcp", "tools", "claude code", "integration"]
},
{
question: "How do I use MCP tools with HTTP+SSE streaming?",
category: "mcp_usage",
keywords: ["mcp", "usage", "streaming", "http", "sse", "real-time"]
},
{
question: "What are all 18 Hive AI MCP tools with descriptions?",
category: "mcp_comprehensive",
keywords: ["mcp", "all tools", "complete list", "18 tools"]
},
{
question: "How do I setup Hive AI MCP integration for multiple IDEs?",
category: "setup",
keywords: ["setup", "installation", "getting started", "multi-ide", "universal"]
},
{
question: "What is HTTP+SSE streaming in Hive AI MCP and how does it work?",
category: "mcp_streaming",
keywords: ["http+sse", "streaming", "server-sent events", "real-time", "transport"]
},
{
question: "Which IDEs support Hive AI MCP with streaming?",
category: "mcp_ides",
keywords: ["claude code", "vscode", "cursor", "windsurf", "ides", "streaming"]
},
{
question: "How do I configure MCP server ports and resolve conflicts?",
category: "mcp_ports",
keywords: ["port", "3000", "conflicts", "configuration", "management"]
},
{
question: "What's the difference between stdio and HTTP+SSE MCP transport?",
category: "mcp_transport",
keywords: ["stdio", "http+sse", "transport", "comparison", "streaming"]
},
{
question: "How do I troubleshoot MCP streaming not working in my IDE?",
category: "mcp_troubleshooting",
keywords: ["troubleshoot", "streaming", "not working", "collapsed", "lines"]
},
{
question: "What is Infrastructure as Code in Hive AI?",
category: "iac",
keywords: ["infrastructure", "code", "iac", "config", "yaml"]
},
{
question: "How do I manage costs with Hive AI?",
category: "cost",
keywords: ["cost", "budget", "pricing", "optimization"]
},
{
question: "How does the 4-stage consensus pipeline work with streaming?",
category: "consensus",
keywords: ["consensus", "pipeline", "4-stage", "generator", "refiner", "validator", "curator", "streaming"]
},
{
question: "What expert profile templates are available?",
category: "profiles",
keywords: ["profiles", "templates", "expert", "presets"]
},
{
question: "How do I start and manage the MCP HTTP server?",
category: "mcp_server",
keywords: ["mcp server", "start", "stop", "status", "http", "management"]
},
{
question: "What security features are available in HTTP+SSE MCP transport?",
category: "mcp_security",
keywords: ["security", "cors", "rate limiting", "session", "authentication"]
},
{
question: "How do I manage cache in Hive AI and fix NPX caching issues?",
category: "cache_management",
keywords: ["cache", "clear", "npx", "npm", "performance", "troubleshooting", "quickstart", "version", "update"]
}
];
// Generate seed entries
for (const topic of knowledgeTopics) {
// Get intelligent response from knowledge base
const response = await this.knowledgeBase.generateHelpResponse(topic.question, 'mcp', 'beginner');
// Create conversation record
const conversationId = uuidv4();
const knowledgeId = uuidv4();
// Insert main conversation
await db.run(`
INSERT INTO conversations (
id, consensus_profile_id, created_at, updated_at
) VALUES (?, ?, datetime('now'), datetime('now'))
`, [
conversationId,
null // No specific consensus profile for seeded knowledge
]);
// Insert knowledge conversation
await db.run(`
INSERT INTO knowledge_conversations (
id, conversation_id, question, final_answer,
source_of_truth, conversation_context, profile_id,
created_at, last_updated
) VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
`, [
knowledgeId,
conversationId,
topic.question,
response,
'Knowledge Base Seed v1.0',
JSON.stringify({ category: topic.category, seeded: true }),
'knowledge_base_seed'
]);
// Insert curator truth for searchability
await db.run(`
INSERT INTO curator_truths (
conversation_id, curator_output, confidence_score,
topic_summary, created_at
) VALUES (?, ?, ?, ?, datetime('now'))
`, [
conversationId,
response,
1.0, // Maximum confidence for seeded knowledge
topic.category
]);
// Insert keywords for better search
for (const keyword of topic.keywords) {
await db.run(`
INSERT INTO conversation_keywords (
conversation_id, keyword, frequency, created_at
) VALUES (?, ?, ?, datetime('now'))
`, [
conversationId,
keyword,
1
]);
}
console.log(` ✅ Seeded: ${topic.category}`);
}
// Set configuration flags including version-specific flag
const currentVersion = await this.getCurrentVersion();
await setConfig('knowledge_base_seeded', 'true');
await setConfig('knowledge_base_seed_version', '1.0');
await setConfig('knowledge_base_seed_date', new Date().toISOString());
await setConfig(`knowledge_base_seeded_v${currentVersion}`, 'true');
console.log('🎉 Knowledge base seeding complete!');
console.log('💡 New users will now have immediate access to:');
console.log(' • All 18 MCP tool documentation with HTTP+SSE streaming');
console.log(' • Multi-IDE setup guides (Claude Code, VS Code, Cursor, Windsurf)');
console.log(' • HTTP+SSE streaming configuration and troubleshooting');
console.log(' • MCP server management (start/stop/port configuration)');
console.log(' • Infrastructure as Code examples with MCP transport settings');
console.log(' • Real-time consensus pipeline with streaming documentation');
console.log(' • Cost management strategies and optimization');
console.log(' • Expert profile templates and security configurations');
console.log(' • Transport comparison (stdio vs HTTP+SSE) and migration guides');
}
catch (error) {
// Check if it's a read-only database error (common in global npm installs)
if (error.code === 'SQLITE_READONLY') {
console.debug('Database is read-only, skipping knowledge base seeding');
return;
}
// For other errors, log but don't throw
console.debug('Knowledge base seeding skipped:', error.message);
}
}
/**
* Get seeded knowledge for a query
*/
async getSeededKnowledge(query) {
try {
const db = await getDatabase();
// Search in seeded knowledge
const results = await db.all(`
SELECT
kc.question,
kc.final_answer,
kc.source_of_truth,
ct.confidence_score,
ct.topic_summary
FROM knowledge_conversations kc
JOIN curator_truths ct ON kc.conversation_id = ct.conversation_id
WHERE kc.source_of_truth LIKE '%Knowledge Base Seed%'
AND (
kc.question LIKE '%' || ? || '%'
OR kc.final_answer LIKE '%' || ? || '%'
OR ct.curator_output LIKE '%' || ? || '%'
)
ORDER BY ct.confidence_score DESC
LIMIT 5
`, [query, query, query]);
return results;
}
catch (error) {
console.error('Error retrieving seeded knowledge:', error);
return [];
}
}
/**
* Update seed with new knowledge base content
*/
async updateSeed() {
console.log('🔄 Updating knowledge base seed...');
// Mark old seeds as outdated
const db = await getDatabase();
await db.run(`
UPDATE knowledge_conversations
SET source_of_truth = source_of_truth || ' (Outdated)'
WHERE source_of_truth LIKE '%Knowledge Base Seed%'
AND source_of_truth NOT LIKE '%(Outdated)%'
`);
// Re-seed with updated content
await this.seedKnowledgeBase();
}
}
/**
* Run seeding during initialization
*/
export async function ensureKnowledgeBaseSeeded() {
try {
const seeder = new KnowledgeBaseSeeder();
await seeder.seedKnowledgeBase();
}
catch (error) {
// Don't let seeding failures interrupt setup process
if (error.code === 'SQLITE_READONLY' || error.message?.includes('read-only')) {
console.debug('Knowledge base seeding skipped: database is read-only');
}
else if (error.message?.includes('Database directory not accessible')) {
console.debug('Knowledge base seeding skipped: database directory not accessible');
}
else {
console.debug('Knowledge base seeding skipped:', error.message);
}
}
}
/**
* Search seeded knowledge
*/
export async function searchSeededKnowledge(query) {
const seeder = new KnowledgeBaseSeeder();
return seeder.getSeededKnowledge(query);
}
//# sourceMappingURL=knowledge-base-seeder.js.map