Spaces:
Runtime error

File size: 7,395 Bytes
d516e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f568a4f
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
import type { 
  Candidate, AngelSlot, TarotSpreadItem, Aura, MergedNexus, 
  TemporalSynthesis, EngineOutput, Insights, Milestone 
} from './types';

const PEACE_LEX = ['peace', 'calm', 'harmony', 'balance', 'gentle', 'serene', 'tranquil'];
const JOY_LEX = ['joy', 'abundance', 'success', 'celebration', 'prosperity', 'fulfillment'];

function containsAny(text: string, words: string[]): boolean {
  return words.some(w => text.includes(w));
}

export function generateTLID(name: string, dob: string, date: string): string {
  const input = `${name}+${dob}+${date}+Nexus:10-9-1+Peace+Joy`;
  const hash = btoa(input).slice(0, 16).toUpperCase();
  const initials = name.split(' ').map(n => n[0]).join('');
  return `NX-${date.replace(/-/g, '')}-${initials}-${hash.slice(0,4)}-${hash.slice(4,8)}-${hash.slice(8,12)}-${hash.slice(12,16)}`;
}

export function enumerateOutcomes(angelSlots: AngelSlot[], tarot: TarotSpreadItem[], nexus: MergedNexus[]): Candidate[] {
  const out: Candidate[] = [];
  
  angelSlots.forEach(slot => {
    const actions = [`Follow ${slot.card.name} guidance`, 'Take aligned action within 48 hours'];
    out.push({
      label: `Angel Path: ${slot.card.name}`,
      actions,
      rationale: slot.card.message || 'Divine guidance pathway',
      features: { alignment: 0, timing: 0, coherence: 0, risk: 0, effort: 0, support: 0 }
    });
  });

  tarot.forEach(item => {
    const message = item.reversed ? item.card.reversed : item.card.upright;
    const actions = [`Apply ${item.card.name} energy`, 'Integrate this wisdom today'];
    out.push({
      label: `Tarot Path: ${item.card.name}`,
      actions,
      rationale: message,
      features: { alignment: 0, timing: 0, coherence: 0, risk: 0, effort: 0, support: 0 }
    });
  });

  nexus.forEach(n => {
    out.push({
      label: `Nexus: ${n.field}`,
      actions: [`Focus on ${n.value}`, 'Align with this energy field'],
      rationale: `Nexus support: ${n.support}`,
      features: { alignment: 0, timing: 0, coherence: 0, risk: 0, effort: 0, support: 0 }
    });
  });

  return out.slice(0, 10);
}

export function enforcePeaceAndJoy(candidates: Candidate[]): Candidate[] {
  return candidates.filter(c => {
    const text = (c.label + ' ' + c.actions.join(' ')).toLowerCase();
    return containsAny(text, PEACE_LEX) || containsAny(text, JOY_LEX) || 
           text.includes('harmony') || text.includes('balance') || text.includes('joy');
  });
}

export function scoreCandidate(c: Candidate, aura: Aura, timing: any, consensusScore: number, tarotReversals: number, signalsSupport: number): Candidate {
  const text = (c.label + ' ' + c.actions.join(' ')).toLowerCase();
  const auraHit = aura.traits.some(t => text.includes(t.toLowerCase()));
  const alignment = auraHit ? 0.9 : 0.6;
  
  const timingScore = 0.7;
  const coherence = Math.min(1, 0.55 + 0.1 * consensusScore);
  const risk = Math.max(0, 1 - 0.1 * tarotReversals);
  const effort = Math.max(0.5, 1 - 0.05 * (c.actions.length - 2));
  
  const resonance = (containsAny(text, PEACE_LEX) ? 0.08 : 0) + (containsAny(text, JOY_LEX) ? 0.08 : 0);
  const support = Math.min(1, 0.6 + 0.05 * signalsSupport + resonance);
  
  c.features = { alignment, timing: timingScore, coherence, risk, effort, support };
  
  const weights = [0.28, 0.22, 0.18, 0.14, 0.08, 0.10];
  const scores = [alignment, timingScore, coherence, risk, effort, support];
  c.score = scores.reduce((sum, score, i) => sum + (weights[i] || 0) * score, 0);
  
  return c;
}

function softmax(scores: number[]): number[] {
  const max = Math.max(...scores);
  const exps = scores.map(s => Math.exp(s - max));
  const sum = exps.reduce((a, b) => a + b, 0);
  return exps.map(e => e / sum);
}

export function temporalSynthesis(
  angelSlots: AngelSlot[], 
  tarot: TarotSpreadItem[], 
  aura: Aura, 
  timing: any, 
  consensusScore: number, 
  mergedNexus: MergedNexus[], 
  observerIntent?: string
): TemporalSynthesis {
  const tarotReversals = tarot.filter(t => t.reversed).length;
  const signalsSupport = mergedNexus.reduce((acc, n) => acc + (n.support || 0), 0);

  let allCandidates = enumerateOutcomes(angelSlots, tarot, mergedNexus);
  
  let candidates = enforcePeaceAndJoy(allCandidates);
  if (!candidates.length) {
    candidates = [{
      label: 'Restore Harmony & Invite Joy',
      actions: ['Resolve one tension kindly', 'Create a small joy moment'],
      window: { tag: 'near' },
      rationale: 'Synthesized directive anchors calm, constructive path',
      features: { alignment: 0.8, timing: 0.7, coherence: 0.9, risk: 0.2, effort: 0.6, support: 0.9 }
    }];
  }

  candidates = candidates.map(c => scoreCandidate(c, aura, timing, consensusScore, tarotReversals, signalsSupport));

  const intentBoost = 0.1;
  candidates.forEach(c => {
    const intentMatch = observerIntent && c.label.toLowerCase().includes(observerIntent.toLowerCase()) ? 1 : 0;
    c.score = (c.score || 0) * (1 + intentBoost * intentMatch);
  });

  const scores = candidates.map(c => c.score || 0);
  const probabilities = softmax(scores);
  
  candidates.forEach((c, i) => {
    c.confidence = probabilities[i];
  });

  const bestIndex = probabilities.indexOf(Math.max(...probabilities));
  const best = candidates[bestIndex];
  const alternates = candidates.filter((_, i) => i !== bestIndex).slice(0, 2);

  const directive = `Prime Timeline: ${best.label} - ${best.actions[0]}`;

  const diagnostics = candidates.map(c => ({
    label: c.label,
    confidence: c.confidence || 0,
    features: c.features
  }));

  return { best, alternates, directive, diagnostics };
}

export function runFiniteEngine(
  name: string,
  dob: string,
  intent: string,
  angelSlots: AngelSlot[],
  tarot: TarotSpreadItem[],
  aura: Aura,
  mergedNexus: MergedNexus[]
): EngineOutput {
  const today = new Date().toISOString().split('T')[0];
  const tlid = generateTLID(name, dob, today);
  
  const timing = { A: { window: 'near' }, B: { window: 'later' } };
  const consensusScore = 0.8;
  
  const tms = temporalSynthesis(angelSlots, tarot, aura, timing, consensusScore, mergedNexus, intent);
  
  const insights: Insights = {
    consensus_score: consensusScore,
    tensions: ['Balance work and rest', 'Align action with values'],
    opportunities: ['New creative projects', 'Relationship harmony'],
    risks: ['Overcommitment', 'Neglecting self-care'],
    affirmation: 'I move forward with clarity and peace',
    journal_prompts: ['What brings me joy today?', 'How can I create more harmony?'],
    micro_advice: angelSlots.slice(0, 3).map(slot => ({
      slot: slot.slot,
      card: slot.card.name,
      tip: 'Trust your intuition'
    }))
  };

  const milestones: Milestone[] = [
    { label: 'Immediate Action', start: today, suggestion: tms.best.actions[0] },
    { label: 'Weekly Review', suggestion: 'Assess progress and adjust course' }
  ];

  return {
    tlid,
    aura,
    angel: { spread: angelSlots, timing, merged_nexus: mergedNexus },
    tarot: { spread: tarot },
    synthesis: tms.directive,
    plan: {
      day1: [tms.best.actions[0] || 'Focus on alignment'],
      day2: [tms.best.actions[1] || 'Take inspired action'],
      day3: ['Review and integrate insights']
    },
    insights,
    milestones,
    tms,
    nexus_source_law: '10-9-1: Ten outcomes, nine harmonic filters, one prime directive',
    constraints: { peace_and_joy: true }
  };
}