/**
* Database schema for PT-MCP Knowledge Graph
*
* Based on Ludwig neurosymbolic system patterns with triple store
* for YAGO entities and Schema.org annotations.
*/
/**
* SQL schema for knowledge graph storage
* Using sql.js (SQLite WASM) for pure JavaScript implementation
*/
export const SCHEMA_SQL = `
-- Core entities from codebase analysis
CREATE TABLE IF NOT EXISTS entities (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT NOT NULL, -- 'framework', 'library', 'language', 'concept', etc.
source_file TEXT,
metadata TEXT, -- JSON string with additional metadata
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_entities_name ON entities(name);
CREATE INDEX idx_entities_type ON entities(type);
-- YAGO knowledge graph mappings
CREATE TABLE IF NOT EXISTS yago_mappings (
entity_id INTEGER PRIMARY KEY,
yago_uri TEXT NOT NULL UNIQUE,
yago_type TEXT, -- Schema.org type from YAGO
confidence REAL CHECK(confidence >= 0 AND confidence <= 1),
facts TEXT, -- JSON string with YAGO facts/relationships
cached_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX idx_yago_uri ON yago_mappings(yago_uri);
CREATE INDEX idx_yago_confidence ON yago_mappings(confidence);
-- Schema.org annotations
CREATE TABLE IF NOT EXISTS schema_annotations (
entity_id INTEGER PRIMARY KEY,
schema_type TEXT NOT NULL, -- e.g., 'SoftwareApplication', 'WebAPI'
properties TEXT, -- JSON string with Schema.org properties
context_url TEXT DEFAULT 'https://schema.org',
FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);
CREATE INDEX idx_schema_type ON schema_annotations(schema_type);
-- Query result cache for SPARQL queries
CREATE TABLE IF NOT EXISTS query_cache (
query_hash TEXT PRIMARY KEY,
query TEXT NOT NULL,
results TEXT NOT NULL, -- JSON string with query results
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL
);
CREATE INDEX idx_query_expires ON query_cache(expires_at);
-- Programming knowledge graph (custom domain-specific)
CREATE TABLE IF NOT EXISTS programming_concepts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
concept_name TEXT NOT NULL UNIQUE,
category TEXT NOT NULL, -- 'pattern', 'paradigm', 'architecture', 'testing', etc.
description TEXT,
related_concepts TEXT, -- JSON array of related concept IDs
examples TEXT, -- JSON array of code examples
metadata TEXT -- JSON with additional properties
);
CREATE INDEX idx_programming_category ON programming_concepts(category);
-- RDF triples for local knowledge graph (optional, for N3.js fallback)
CREATE TABLE IF NOT EXISTS rdf_triples (
id INTEGER PRIMARY KEY AUTOINCREMENT,
subject TEXT NOT NULL,
predicate TEXT NOT NULL,
object TEXT NOT NULL,
graph TEXT DEFAULT 'default',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_rdf_subject ON rdf_triples(subject);
CREATE INDEX idx_rdf_predicate ON rdf_triples(predicate);
CREATE INDEX idx_rdf_object ON rdf_triples(object);
`;
/**
* Entity types supported by PT-MCP
*/
export enum EntityType {
FRAMEWORK = 'framework',
LIBRARY = 'library',
LANGUAGE = 'language',
CONCEPT = 'concept',
PATTERN = 'pattern',
TOOL = 'tool',
API = 'api',
DATABASE = 'database',
}
/**
* Schema.org types commonly used in software context
*/
export enum SchemaOrgType {
SOFTWARE_APPLICATION = 'SoftwareApplication',
SOFTWARE_SOURCE_CODE = 'SoftwareSourceCode',
WEB_APPLICATION = 'WebApplication',
WEB_API = 'WebAPI',
API_REFERENCE = 'APIReference',
COMPUTER_LANGUAGE = 'ComputerLanguage',
TECH_ARTICLE = 'TechArticle',
HOW_TO = 'HowTo',
CREATIVE_WORK = 'CreativeWork',
}
/**
* Interface for entity records
*/
export interface Entity {
id?: number;
name: string;
type: EntityType | string;
source_file?: string;
metadata?: Record<string, any>;
created_at?: Date;
}
/**
* Interface for YAGO mapping records
*/
export interface YAGOMapping {
entity_id: number;
yago_uri: string;
yago_type?: string;
confidence: number;
facts?: Record<string, any>;
cached_at?: Date;
}
/**
* Interface for Schema.org annotation records
*/
export interface SchemaAnnotation {
entity_id: number;
schema_type: SchemaOrgType | string;
properties: Record<string, any>;
context_url?: string;
}
/**
* Interface for query cache records
*/
export interface QueryCache {
query_hash: string;
query: string;
results: any;
created_at?: Date;
expires_at: Date;
}
/**
* Interface for programming concept records
*/
export interface ProgrammingConcept {
id?: number;
concept_name: string;
category: string;
description?: string;
related_concepts?: number[];
examples?: string[];
metadata?: Record<string, any>;
}
/**
* Interface for RDF triple records
*/
export interface RDFTriple {
id?: number;
subject: string;
predicate: string;
object: string;
graph?: string;
created_at?: Date;
}