File size: 8,022 Bytes
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a84b07b
 
 
 
 
 
529090e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// DataIngestionEngine – Autonomous data collection and enrichment
import { projectMemory } from '../project/ProjectMemory.js';
import { eventBus } from '../../mcp/EventBus.js';
import { dataSourceConfig } from './DataSourceConfigManager.js';

export interface DataSourceAdapter {
    name: string;
    type: 'local_files' | 'outlook_mail' | 'browser_history' | 'google_drive' | 'other';

    /** Fetch raw data from the source */
    fetch(): Promise<any[]>;

    /** Transform raw data into normalized entities */
    transform(raw: any[]): Promise<IngestedEntity[]>;

    /** Health check */
    isAvailable(): Promise<boolean>;
}

export interface IngestedEntity {
    id: string;
    type: string;
    source: string;
    title?: string;
    content?: string;
    metadata: Record<string, any>;
    timestamp: Date;
}

export class DataIngestionEngine {
    private adapters: Map<string, DataSourceAdapter> = new Map();
    private isRunning: boolean = false;
    private ingestedCount: number = 0;

    /** Register a data source adapter */
    async registerAdapter(adapter: DataSourceAdapter, description: string, requiresApproval: boolean = false): Promise<void> {
        this.adapters.set(adapter.name, adapter);
        const canUse = await dataSourceConfig.registerSource(adapter.name, description, requiresApproval);

        console.log(`📥 Registered data adapter: ${adapter.name} (${adapter.type}) - ${canUse ? 'Ready' : 'Awaiting approval'}`);
    }

    /** Get a registered adapter */
    getAdapter(name: string): DataSourceAdapter | undefined {
        return this.adapters.get(name);
    }


    /** Start ingestion from all registered adapters */
    async ingestAll(): Promise<void> {
        if (this.isRunning) {
            console.warn('⚠️ Ingestion already running');
            return;
        }

        // Load config
        await dataSourceConfig.load();

        this.isRunning = true;
        this.ingestedCount = 0;

        console.log(`🚀 Starting data ingestion from ${this.adapters.size} sources...`);

        projectMemory.logLifecycleEvent({
            eventType: 'other',
            status: 'in_progress',
            details: {
                type: 'data_ingestion_started',
                sources: Array.from(this.adapters.keys())
            }
        });

        const results: any[] = [];

        for (const [name, adapter] of this.adapters) {
            try {
                // Check if source is enabled
                if (!dataSourceConfig.isEnabled(name)) {
                    console.log(`⏭️ ${name} is disabled, skipping`);
                    results.push({ source: name, status: 'skipped', reason: 'disabled' });
                    continue;
                }

                console.log(`📂 Ingesting from: ${name}...`);

                // Check availability
                const available = await adapter.isAvailable();
                if (!available) {
                    console.warn(`⚠️ ${name} not available, skipping`);
                    results.push({ source: name, status: 'skipped', reason: 'not_available' });
                    continue;
                }

                // Fetch raw data
                const rawData = await adapter.fetch();
                console.log(`  → Fetched ${rawData.length} items from ${name}`);

                // Transform to normalized entities
                const entities = await adapter.transform(rawData);
                console.log(`  → Transformed ${entities.length} entities`);

                // Store entities (for now, just log - later we'll save to memory/database)
                this.ingestedCount += entities.length;

                // Auto-add to Vidensarkiv (Knowledge Archive) for continuous learning
                try {
                    const { getVectorStore } = await import('../../platform/vector/index.js');
                    const vectorStore = await getVectorStore();

                    // Batch add entities to vidensarkiv
                    const vectorRecords = entities.map(entity => ({
                        id: entity.id,
                        content: entity.content || entity.title || JSON.stringify(entity.metadata),
                        embedding: [], // Will be generated automatically
                        metadata: {
                            ...entity.metadata,
                            datasetType: 'new',
                            source: name,
                            type: entity.type,
                            ingestedAt: new Date().toISOString()
                        },
                        namespace: `org:default:user:system` // TODO: Get from context
                    }));

                    if (vectorRecords.length > 0) {
                        await vectorStore.batchUpsert({
                            records: vectorRecords,
                            namespace: `org:default:user:system`
                        });
                        console.log(`  → Added ${vectorRecords.length} entities to vidensarkiv`);
                    }
                } catch (err) {
                    console.warn(`⚠️ Failed to add to vidensarkiv:`, err);
                    // Non-critical, continue ingestion
                }

                results.push({
                    source: name,
                    status: 'success',
                    items: entities.length
                });

                // Mark as used
                await dataSourceConfig.markUsed(name);

                // Emit event for real-time updates
                // Emit event for real-time updates
                eventBus.emit('data:ingested', {
                    source: name,
                    count: entities.length,
                    entities: entities // Send ALL entities so IngestionPipeline can process them
                });

            } catch (error: any) {
                console.error(`❌ Failed to ingest from ${name}:`, error.message);
                results.push({
                    source: name,
                    status: 'error',
                    error: error.message
                });
            }
        }

        this.isRunning = false;

        // Log completion
        projectMemory.logLifecycleEvent({
            eventType: 'other',
            status: 'success',
            details: {
                type: 'data_ingestion_completed',
                totalIngested: this.ingestedCount,
                results
            }
        });

        console.log(`✅ Data ingestion complete! Total entities: ${this.ingestedCount}`);
    }

    /** Ingest from a specific source */
    async ingestFrom(sourceName: string): Promise<number> {
        const adapter = this.adapters.get(sourceName);

        if (!adapter) {
            throw new Error(`Unknown data source: ${sourceName}`);
        }

        // Check if enabled
        if (!dataSourceConfig.isEnabled(sourceName)) {
            throw new Error(`Data source ${sourceName} is not enabled`);
        }

        const available = await adapter.isAvailable();
        if (!available) {
            throw new Error(`Source ${sourceName} is not available`);
        }

        const rawData = await adapter.fetch();
        const entities = await adapter.transform(rawData);

        await dataSourceConfig.markUsed(sourceName);

        eventBus.emit('data:ingested', {
            source: sourceName,
            count: entities.length,
            entities: entities.slice(0, 5)
        });

        return entities.length;
    }

    /** Get ingestion status */
    getStatus() {
        return {
            running: this.isRunning,
            totalIngested: this.ingestedCount,
            adapters: Array.from(this.adapters.keys()),
            enabled: dataSourceConfig.getEnabledSources(),
            pendingApprovals: dataSourceConfig.getPendingApprovals()
        };
    }
}

// Singleton instance
export const dataIngestionEngine = new DataIngestionEngine();