All files / lib/conscious insight_engine.ts

0% Statements 0/27
0% Branches 0/1
0% Functions 0/1
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                 
/**
 * يولّد "أفكار" بشرية من الخريطة السببية.
 */
 
import fs from 'node:fs';
import { ConsciousConfig } from '../../config/conscious.config';
 
export function generateInsights(causalMap) {
  const insights = causalMap.map((link) => {
    const direction = link.cause === 'positive' ? 'تحسّن' : 'تدهور';
    return {
      policyId: link.policyId,
      summary: `السياسة ${link.policyId} أدت إلى ${direction} بمقدار ${Math.abs(
        link.deltaScore
      ).toFixed(3)}.`,
      weightChange: link.deltaWeight.toFixed(3),
      correlation: link.correlation.toFixed(3),
      timestamp: new Date().toISOString(),
    };
  });
 
  persistInsights(insights);
  return insights;
}
 
function persistInsights(insights) {
  fs.mkdirSync('./storage', { recursive: true });
  const path = ConsciousConfig.storagePath;
  const existing = fs.existsSync(path) ? JSON.parse(fs.readFileSync(path, 'utf8')) : [];
  const merged = [...existing, ...insights].slice(-ConsciousConfig.maxInsights);
  fs.writeFileSync(path, JSON.stringify(merged, null, 2));
}