"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KnowledgeGraphService = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
/**
* Service for interacting with the Knowledge Graph Memory Server
*/
class KnowledgeGraphService {
/**
* Initialize the Knowledge Graph service
* @param baseUrl - Base URL of the Knowledge Graph Memory Server
*/
constructor(baseUrl) {
this.baseUrl = baseUrl;
// Trim trailing slash if present
this.baseUrl = this.baseUrl.replace(/\/$/, '');
console.log(`Knowledge Graph service initialized with URL: ${this.baseUrl}`);
}
/**
* Check if the Knowledge Graph Memory Server is available
* @returns Whether the server is available
*/
async isAvailable() {
try {
const response = await (0, node_fetch_1.default)(`${this.baseUrl}/health`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
return false;
}
const data = await response.json();
return data.status === 'ok';
}
catch (error) {
console.error('Error checking Knowledge Graph availability:', error instanceof Error ? error.message : String(error));
return false;
}
}
/**
* Search for entities in the Knowledge Graph
* @param query - Search query
* @returns Search results
*/
async searchNodes(query) {
try {
const response = await (0, node_fetch_1.default)(`${this.baseUrl}/search-nodes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query })
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Search failed: ${response.status} ${errorText}`);
}
return await response.json();
}
catch (error) {
console.error('Error searching Knowledge Graph:', error instanceof Error ? error.message : String(error));
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Get the complete Knowledge Graph
* @returns The complete Knowledge Graph
*/
async getGraph() {
try {
const response = await (0, node_fetch_1.default)(`${this.baseUrl}/read-graph`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to get graph: ${response.status} ${errorText}`);
}
return await response.json();
}
catch (error) {
console.error('Error getting Knowledge Graph:', error instanceof Error ? error.message : String(error));
return null;
}
}
/**
* Get specific nodes from the Knowledge Graph by their names
* @param names - Array of node names to retrieve
* @returns The requested nodes and their relations
*/
async getNodes(names) {
try {
const response = await (0, node_fetch_1.default)(`${this.baseUrl}/open-nodes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ names })
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to get nodes: ${response.status} ${errorText}`);
}
return await response.json();
}
catch (error) {
console.error('Error getting nodes from Knowledge Graph:', error instanceof Error ? error.message : String(error));
return null;
}
}
/**
* Extract development framework context from Knowledge Graph entities
* @param graph - Knowledge Graph data to extract from
* @returns Development context extracted from entities
*/
extractDevelopmentContext(graph) {
let contextText = '';
if (!graph || !graph.entities || graph.entities.length === 0) {
return 'No development frameworks found in Knowledge Graph.';
}
// Find development framework entities
const frameworkEntities = graph.entities.filter(entity => entity.entityType.toLowerCase().includes('framework') ||
entity.entityType.toLowerCase().includes('workflow') ||
entity.entityType.toLowerCase().includes('development') ||
entity.entityType.toLowerCase().includes('pattern'));
if (frameworkEntities.length === 0) {
return 'No development frameworks found in Knowledge Graph.';
}
// Get related relations
const frameworkRelations = graph.relations.filter(relation => frameworkEntities.some(entity => entity.name === relation.from || entity.name === relation.to));
// Build context text from entities
contextText += '# Development Framework from Knowledge Graph\n\n';
frameworkEntities.forEach(entity => {
contextText += `## ${entity.name} (${entity.entityType})\n\n`;
// Add observations as context
if (entity.observations && entity.observations.length > 0) {
entity.observations.forEach(observation => {
contextText += `${observation}\n\n`;
});
}
// Add related entities through relations
const relatedRelations = frameworkRelations.filter(relation => relation.from === entity.name || relation.to === entity.name);
if (relatedRelations.length > 0) {
contextText += '### Related Components:\n\n';
relatedRelations.forEach(relation => {
if (relation.from === entity.name) {
contextText += `- ${relation.relationType} → ${relation.to}\n`;
}
else {
contextText += `- ${relation.from} → ${relation.relationType} → ${entity.name}\n`;
}
});
contextText += '\n';
}
});
return contextText;
}
}
exports.KnowledgeGraphService = KnowledgeGraphService;
//# sourceMappingURL=knowledge-graph.service.js.map