File size: 3,065 Bytes
ea270a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { computeLiveMetrics } from '../data/vessels-thresholds';

const STORAGE_KEY = 'vessels_brief_signal';
const AUTO_FIRED_KEY = 'vessels_auto_brief_fired';

export interface BriefSignal {
  query: string;
  context: string;
  source: string;
  firedAt: string;
  autonomous?: boolean;
}

export function fireBriefSignal(signal: Omit<BriefSignal, 'firedAt'>) {
  const payload: BriefSignal = { ...signal, firedAt: new Date().toISOString() };
  try {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
  } catch {}
}

export function consumeBriefSignal(): BriefSignal | null {
  try {
    const raw = localStorage.getItem(STORAGE_KEY);
    if (!raw) return null;
    localStorage.removeItem(STORAGE_KEY);
    return JSON.parse(raw) as BriefSignal;
  } catch {
    return null;
  }
}

export function getAutonomousSignal(): BriefSignal | null {
  try {
    const alreadyFired = localStorage.getItem(AUTO_FIRED_KEY);
    if (alreadyFired) return null;

    const { maxHormuzProbability, avgSuspicionScore, hormuzZone } = computeLiveMetrics();

    if (maxHormuzProbability >= 80 && hormuzZone) {
      localStorage.setItem(AUTO_FIRED_KEY, 'hormuz-probability');
      return {
        query: `Generate a maritime intelligence brief for: ${hormuzZone.name}${hormuzZone.severity} disruption zone with ${hormuzZone.probability72h}% probability of impact in the next 72 hours. ${hormuzZone.affectedVessels} commercial vessels affected. Aggregate cargo value ${hormuzZone.cargoValue}. Provide situation summary, affected parties, dollar impact estimate, and 3 recommended actions for charterers and P&I clubs.`,
        context: `Autonomous rule trigger — ${hormuzZone.name} disruption probability exceeded 80% threshold (live value: ${maxHormuzProbability}%)`,
        source: `Autonomous Alert — Disruption Forecast Engine (${hormuzZone.name} ${maxHormuzProbability}% ≥ 80% threshold)`,
        firedAt: new Date().toISOString(),
        autonomous: true,
      };
    }

    if (avgSuspicionScore >= 85) {
      localStorage.setItem(AUTO_FIRED_KEY, 'dark-fleet-suspicion');
      return {
        query: `Generate a maritime intelligence brief for: Dark fleet monitoring alert — average vessel suspicion score is ${avgSuspicionScore}/100 across active monitoring, exceeding the 85/100 high-risk threshold. Multiple vessels with extended AIS gaps detected. Provide a sanctions compliance briefing, insurance implications, and 3 recommended actions for compliance officers and P&I underwriters.`,
        context: `Autonomous rule trigger — dark fleet average suspicion score ${avgSuspicionScore}/100 exceeded threshold 85/100 (live value from active vessel monitoring)`,
        source: `Autonomous Alert — Dark Fleet Economics (avg suspicion ${avgSuspicionScore}/100 ≥ 85 threshold)`,
        firedAt: new Date().toISOString(),
        autonomous: true,
      };
    }

    return null;
  } catch {
    return null;
  }
}

export function resetAutonomousSignals() {
  try {
    localStorage.removeItem(AUTO_FIRED_KEY);
  } catch {}
}