File size: 1,956 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { db } from '../config/gun.js';
import { gunSafe } from '../utils/gunUtils.js';

/**
 * SynthesisService — Phase 25: Knowledge Synthesis
 * 
 * Extracts "Atomic Facts" from papers promoted to La Rueda 
 * and builds a persistent Hive Knowledge Graph (HKG).
 */

class SynthesisService {
    /**
     * Extracts facts from a paper and stores them in the graph.
     * In a full implementation, this would use an LLM or NLP pipeline.
     */
    async synthesizePaper(paper) {
        if (!paper.content) return;

        // Simple heuristic fact extraction (Phase 25 initial version)
        // Looks for sentences containing "is", "proves", "demonstrates"
        const facts = paper.content
            .split('.')
            .map(s => s.trim())
            .filter(s => s.length > 30 && (s.includes('proves') || s.includes('demonstrates') || s.includes('shows')));

        for (const factText of facts) {
            const factId = `fact-${Math.random().toString(36).substring(2, 8)}`;
            const atomicFact = {
                id: factId,
                subject: paper.title,
                predicate: 'demonstrates',
                content: factText,
                sourcePaperId: paper.id || 'unknown',
                confidence: parseFloat(paper.occam_score || 0.8),
                timestamp: Date.now()
            };

            db.get('knowledge_graph').get(factId).put(gunSafe(atomicFact));
            console.log(`[SYNTHESIS] Atomic fact extracted: ${factId}`);
        }
    }

    /**
     * Returns the current state of the Hive Knowledge Graph.
     */
    async getKnowledgeGraph() {
        return new Promise((resolve) => {
            const graph = [];
            db.get('knowledge_graph').map().once((fact) => {
                if (fact) graph.push(fact);
            });
            setTimeout(() => resolve(graph), 1000);
        });
    }
}

export const synthesisService = new SynthesisService();