import { TopologyModel, type TopologyModelOptions, type TopologyEntity, type TopologyRelationship } from "./TopologyModel.ts"; export type KnowledgeEntityType = | "page" | "entry" | "incident" | "organization" | "person" | "skill" | "technology" | "project" | "section" | "custom"; export type KnowledgeRelationshipKind = | "related" | "parent" | "child" | "references" | "mentions" | "depends-on" | "supports" | "contains" | "custom"; export interface KnowledgeEntityInput { id: string; type?: KnowledgeEntityType; title?: string; label?: string; slug?: string; language?: string; subtitle?: string; summary?: string; description?: string; body?: string; tags?: string[]; q?: number; r?: number; relatedIds?: string[]; organizationIds?: string[]; incidentIds?: string[]; personIds?: string[]; metadata?: Record; } export interface KnowledgeRelationshipInput { from: string; to: string; kind?: KnowledgeRelationshipKind; label?: string; active?: boolean; muted?: boolean; } export interface KnowledgeIssue { type: "missing-entity" | "duplicate-entity" | "broken-relationship"; message: string; entityId?: string; relationship?: KnowledgeRelationshipRecord; } export interface KnowledgeEntityRecord { id: string; type: KnowledgeEntityType; title: string; label: string; slug?: string; language?: string; subtitle?: string; summary?: string; description?: string; body?: string; tags: string[]; q?: number; r?: number; metadata: Record; } export interface KnowledgeRelationshipRecord { from: string; to: string; kind: KnowledgeRelationshipKind; label?: string; active?: boolean; muted?: boolean; } export interface KnowledgeRepositorySource { entities: KnowledgeEntityInput[]; relationships?: KnowledgeRelationshipInput[]; } export interface KnowledgeWorldOptions extends Partial { radius?: number; columns?: number; rows?: number; originQ?: number; originR?: number; } function toType(type?: KnowledgeEntityInput["type"]): KnowledgeEntityType { return type ?? "custom"; } function toLabel(entity: KnowledgeEntityInput) { return entity.label ?? entity.title ?? entity.id; } function normalizeEntity(entity: KnowledgeEntityInput): KnowledgeEntityRecord { return { id: entity.id, type: toType(entity.type), title: entity.title ?? entity.label ?? entity.id, label: toLabel(entity), slug: entity.slug, language: entity.language, subtitle: entity.subtitle, summary: entity.summary, description: entity.description, body: entity.body, tags: Array.isArray(entity.tags) ? [...entity.tags] : [], q: typeof entity.q === "number" ? entity.q : undefined, r: typeof entity.r === "number" ? entity.r : undefined, metadata: entity.metadata ? { ...entity.metadata } : {}, }; } function normalizeRelationship(relationship: KnowledgeRelationshipInput): KnowledgeRelationshipRecord { return { from: relationship.from, to: relationship.to, kind: relationship.kind ?? "related", label: relationship.label, active: relationship.active, muted: relationship.muted, }; } export class KnowledgeRepository { private readonly entityMap = new Map(); private readonly relationshipRecords: KnowledgeRelationshipRecord[] = []; private readonly issues: KnowledgeIssue[] = []; constructor(source: KnowledgeRepositorySource) { for (const entity of source.entities) { if (this.entityMap.has(entity.id)) { this.issues.push({ type: "duplicate-entity", message: "Duplicate entity id: " + entity.id, entityId: entity.id, }); continue; } this.entityMap.set(entity.id, normalizeEntity(entity)); } for (const relationship of source.relationships ?? []) { const normalized = normalizeRelationship(relationship); const fromExists = this.entityMap.has(normalized.from); const toExists = this.entityMap.has(normalized.to); if (!fromExists || !toExists) { this.issues.push({ type: "broken-relationship", message: "Broken relationship: " + normalized.from + " -> " + normalized.to, relationship: normalized, }); continue; } this.relationshipRecords.push(normalized); } } get issuesList() { return [...this.issues]; } getEntity(id: string) { return this.entityMap.get(id) ?? null; } getByType(type: KnowledgeEntityType) { return [...this.entityMap.values()].filter((entity) => entity.type === type); } getRelationships(id: string) { return this.relationshipRecords.filter((relationship) => relationship.from === id || relationship.to === id); } getNeighbors(id: string) { return this.getRelationships(id) .map((relationship) => { const neighborId = relationship.from === id ? relationship.to : relationship.from; return this.entityMap.get(neighborId) ?? null; }) .filter((entity): entity is KnowledgeEntityRecord => Boolean(entity)); } getAllEntities() { return [...this.entityMap.values()]; } getAllRelationships() { return [...this.relationshipRecords]; } createWorld(options: KnowledgeWorldOptions = {}) { const entities: TopologyEntity[] = this.getAllEntities() .filter((entity) => typeof entity.q === "number" && typeof entity.r === "number") .map((entity) => ({ id: entity.id, label: entity.label, subtitle: entity.subtitle ?? entity.summary, q: entity.q as number, r: entity.r as number, type: entity.type === "page" || entity.type === "entry" ? "section" : "entity", })); const relationships: TopologyRelationship[] = this.getAllRelationships().map((relationship) => ({ from: relationship.from, to: relationship.to, kind: relationship.kind, active: relationship.active, muted: relationship.muted, })); return new TopologyModel({ radius: options.radius ?? 40, columns: options.columns ?? 28, rows: options.rows ?? 18, originQ: options.originQ ?? 0, originR: options.originR ?? 0, entities, relationships, }); } }